Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Text;
- namespace VoidwalkerEngine.Framework.Utilities
- {
- public class VoidwalkerCipher
- {
- private int _inext;
- private int _inextp;
- private readonly int[] _seedArray = new int[56];
- private int _seed;
- public int Seed
- {
- get
- {
- return this._seed;
- }
- set
- {
- this._seed = value;
- int subtraction = (this._seed == int.MinValue) ? int.MaxValue : Math.Abs(this._seed);
- int mj = 0x9a4ec86 - subtraction;
- this._seedArray[0x37] = mj;
- int mk = 1;
- for (int i = 1; i < 0x37; i++)
- {
- int ii = (0x15 * i) % 0x37;
- this._seedArray[ii] = mk;
- mk = mj - mk;
- if (mk < 0x0)
- {
- mk += int.MaxValue;
- }
- mj = this._seedArray[ii];
- }
- for (int k = 1; k < 0x5; k++)
- {
- for (int i = 1; i < 0x38; i++)
- {
- this._seedArray[i] -= this._seedArray[1 + ((i + 0x1e) % 0x37)];
- if (this._seedArray[i] < 0)
- {
- this._seedArray[i] += int.MaxValue;
- }
- }
- }
- this._inext = 0;
- this._inextp = 21;
- }
- }
- public VoidwalkerCipher()
- {
- }
- public VoidwalkerCipher(string seed)
- {
- this.SetSeed(seed);
- }
- public VoidwalkerCipher(int seed)
- {
- this.SetSeed(seed);
- }
- public VoidwalkerCipher(byte[] seed)
- {
- this.SetSeed(seed);
- }
- public void Reset()
- {
- this.Seed = this.Seed;
- }
- public void Skip(int times)
- {
- for (int i = 0; i < times; i++)
- {
- this.NextOffset();
- }
- }
- public void SetSeed(string seed)
- {
- this.SetSeed(GenerateHashCode(seed));
- }
- public void SetSeed(byte[] seed)
- {
- this.SetSeed(GenerateHashCode(seed));
- }
- public void SetSeed(int seed)
- {
- this.Seed = seed;
- }
- public static int GenerateHashCode(byte[] data)
- {
- if (data == null || data.Length == 0x0)
- {
- return 0x0;
- }
- int hashCode = 0x0;
- for (int i = 0; i < data.Length; i++)
- {
- hashCode = (hashCode << 0x3) | ((hashCode >> 0x1F) ^ data[i]);
- }
- return hashCode;
- }
- private static unsafe int GenerateHashCode(string data)
- {
- fixed (char* str = data)
- {
- int num = 0x15051505;
- int num2 = num;
- int* numPtr = (int*)str;
- for (int i = data.Length; i > 0; i -= 4)
- {
- num = ((num << 0x5) + num + (num >> 0x1f)) ^ numPtr[0];
- if (i <= 2)
- {
- break;
- }
- num2 = ((num2 << 0x5) + num2 + (num2 >> 0x1f)) ^ numPtr[1];
- numPtr += 2;
- }
- return num + (num2 * 0x5d588b65);
- }
- }
- private int NextOffset()
- {
- int locINext = this._inext;
- int locINextp = this._inextp;
- if (++locINext >= 0x38)
- {
- locINext = 1;
- }
- if (++locINextp >= 0x38)
- {
- locINextp = 1;
- }
- int retVal = this._seedArray[locINext] - this._seedArray[locINextp];
- if (retVal == int.MaxValue)
- {
- retVal--;
- }
- if (retVal < 0)
- {
- retVal += int.MaxValue;
- }
- this._seedArray[locINext] = retVal;
- this._inext = locINext;
- this._inextp = locINextp;
- return retVal;
- }
- public byte[] EncryptString(string data)
- {
- byte[] bytes = Encoding.UTF8.GetBytes(data);
- this.TransformBytes(bytes);
- return bytes;
- }
- public string DecryptString(byte[] data)
- {
- this.TransformBytes(data);
- return Encoding.UTF8.GetString(data);
- }
- public void EncryptFile(string inputPath, string outputPath)
- {
- byte[] inputBytes = File.ReadAllBytes(inputPath);
- this.TransformBytes(inputBytes);
- File.WriteAllBytes(outputPath, inputBytes);
- }
- public void DecryptFile(string inputPath, string outputPath)
- {
- byte[] inputBytes = File.ReadAllBytes(inputPath);
- this.TransformBytes(inputBytes);
- File.WriteAllBytes(outputPath, inputBytes);
- }
- public void TransformBytes(byte[] bytes)
- {
- for (int i = 0; i < bytes.Length; i++)
- {
- bytes[i] = this.TransformByte(bytes[i]);
- }
- }
- public byte TransformByte(byte value)
- {
- return (byte)(value ^ this.NextOffset());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement