Advertisement
Krythic

Voidwalker Encryption

Oct 10th, 2022
1,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.66 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4.  
  5. namespace VoidwalkerEngine.Framework.Utilities
  6. {
  7.     public class VoidwalkerCipher
  8.     {
  9.         private int _inext;
  10.         private int _inextp;
  11.         private readonly int[] _seedArray = new int[56];
  12.         private int _seed;
  13.  
  14.         public int Seed
  15.         {
  16.             get
  17.             {
  18.                 return this._seed;
  19.             }
  20.             set
  21.             {
  22.                 this._seed = value;
  23.                 int subtraction = (this._seed == int.MinValue) ? int.MaxValue : Math.Abs(this._seed);
  24.                 int mj = 0x9a4ec86 - subtraction;
  25.                 this._seedArray[0x37] = mj;
  26.                 int mk = 1;
  27.                 for (int i = 1; i < 0x37; i++)
  28.                 {
  29.                     int ii = (0x15 * i) % 0x37;
  30.                     this._seedArray[ii] = mk;
  31.                     mk = mj - mk;
  32.                     if (mk < 0x0)
  33.                     {
  34.                         mk += int.MaxValue;
  35.                     }
  36.                     mj = this._seedArray[ii];
  37.                 }
  38.                 for (int k = 1; k < 0x5; k++)
  39.                 {
  40.                     for (int i = 1; i < 0x38; i++)
  41.                     {
  42.                         this._seedArray[i] -= this._seedArray[1 + ((i + 0x1e) % 0x37)];
  43.                         if (this._seedArray[i] < 0)
  44.                         {
  45.                             this._seedArray[i] += int.MaxValue;
  46.                         }
  47.                     }
  48.                 }
  49.                 this._inext = 0;
  50.                 this._inextp = 21;
  51.             }
  52.         }
  53.  
  54.         public VoidwalkerCipher(string seed)
  55.         {
  56.             this.SetSeed(seed);
  57.         }
  58.  
  59.         public VoidwalkerCipher(int seed)
  60.         {
  61.             this.SetSeed(seed);
  62.         }
  63.  
  64.         public VoidwalkerCipher(byte[] seed)
  65.         {
  66.             this.SetSeed(seed);
  67.         }
  68.  
  69.         public void Reset()
  70.         {
  71.             this.Seed = this.Seed;
  72.         }
  73.  
  74.         public void Skip(int times)
  75.         {
  76.             for (int i = 0; i < times; i++)
  77.             {
  78.                 this.NextOffset();
  79.             }
  80.         }
  81.  
  82.         public void SetSeed(string seed)
  83.         {
  84.             this.SetSeed(GenerateHashCode(seed));
  85.         }
  86.  
  87.         public void SetSeed(byte[] seed)
  88.         {
  89.             this.SetSeed(GenerateHashCode(seed));
  90.         }
  91.  
  92.         public void SetSeed(int seed)
  93.         {
  94.             this.Seed = seed;
  95.         }
  96.  
  97.         public static int GenerateHashCode(byte[] data)
  98.         {
  99.             if (data == null || data.Length == 0x0)
  100.             {
  101.                 return 0x0;
  102.             }
  103.             int hashCode = 0x0;
  104.             for (int i = 0; i < data.Length; i++)
  105.             {
  106.                 hashCode = (hashCode << 0x3) | ((hashCode >> 0x1F) ^ data[i]);
  107.             }
  108.             return hashCode;
  109.         }
  110.  
  111.         public void DecryptBytes(byte[] fileData)
  112.         {
  113.             TransformBytes(fileData);
  114.         }
  115.  
  116.         private static unsafe int GenerateHashCode(string data)
  117.         {
  118.             fixed (char* str = data)
  119.             {
  120.                 int num = 0x15051505;
  121.                 int num2 = num;
  122.                 int* numPtr = (int*)str;
  123.                 for (int i = data.Length; i > 0; i -= 4)
  124.                 {
  125.                     num = ((num << 0x5) + num + (num >> 0x1f)) ^ numPtr[0];
  126.                     if (i <= 2)
  127.                     {
  128.                         break;
  129.                     }
  130.                     num2 = ((num2 << 0x5) + num2 + (num2 >> 0x1f)) ^ numPtr[1];
  131.                     numPtr += 2;
  132.                 }
  133.                 return num + (num2 * 0x5d588b65);
  134.             }
  135.         }
  136.  
  137.         private int NextOffset()
  138.         {
  139.             int locINext = this._inext;
  140.             int locINextp = this._inextp;
  141.             if (++locINext >= 0x38)
  142.             {
  143.                 locINext = 1;
  144.             }
  145.             if (++locINextp >= 0x38)
  146.             {
  147.                 locINextp = 1;
  148.             }
  149.             int retVal = this._seedArray[locINext] - this._seedArray[locINextp];
  150.             if (retVal == int.MaxValue)
  151.             {
  152.                 retVal--;
  153.             }
  154.             if (retVal < 0)
  155.             {
  156.                 retVal += int.MaxValue;
  157.             }
  158.             this._seedArray[locINext] = retVal;
  159.             this._inext = locINext;
  160.             this._inextp = locINextp;
  161.             return retVal;
  162.         }
  163.  
  164.         public byte[] EncryptString(string data)
  165.         {
  166.             byte[] bytes = Encoding.UTF8.GetBytes(data);
  167.             this.TransformBytes(bytes);
  168.             return bytes;
  169.         }
  170.  
  171.         public string DecryptString(byte[] data)
  172.         {
  173.             this.TransformBytes(data);
  174.             return Encoding.UTF8.GetString(data);
  175.         }
  176.  
  177.         public void EncryptFile(string inputPath, string outputPath)
  178.         {
  179.             byte[] inputBytes = File.ReadAllBytes(inputPath);
  180.             this.TransformBytes(inputBytes);
  181.             File.WriteAllBytes(outputPath, inputBytes);
  182.         }
  183.  
  184.         public void DecryptFile(string inputPath, string outputPath)
  185.         {
  186.             byte[] inputBytes = File.ReadAllBytes(inputPath);
  187.             this.TransformBytes(inputBytes);
  188.             File.WriteAllBytes(outputPath, inputBytes);
  189.         }
  190.  
  191.         public void TransformBytes(byte[] bytes)
  192.         {
  193.             for (int i = 0; i < bytes.Length; i++)
  194.             {
  195.                 bytes[i] = this.TransformByte(bytes[i]);
  196.             }
  197.         }
  198.  
  199.         public byte TransformByte(byte value)
  200.         {
  201.             return (byte)(value ^ this.NextOffset());
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement