Advertisement
Krythic

sfsfsdf

Aug 27th, 2021
1,548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.60 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()
  55.         {
  56.         }
  57.  
  58.         public VoidwalkerCipher(string seed)
  59.         {
  60.             this.SetSeed(seed);
  61.         }
  62.  
  63.         public VoidwalkerCipher(int seed)
  64.         {
  65.             this.SetSeed(seed);
  66.         }
  67.  
  68.         public VoidwalkerCipher(byte[] seed)
  69.         {
  70.             this.SetSeed(seed);
  71.         }
  72.  
  73.         public void Reset()
  74.         {
  75.             this.Seed = this.Seed;
  76.         }
  77.  
  78.         public void Skip(int times)
  79.         {
  80.             for (int i = 0; i < times; i++)
  81.             {
  82.                 this.NextOffset();
  83.             }
  84.         }
  85.  
  86.         public void SetSeed(string seed)
  87.         {
  88.             this.SetSeed(GenerateHashCode(seed));
  89.         }
  90.  
  91.         public void SetSeed(byte[] seed)
  92.         {
  93.             this.SetSeed(GenerateHashCode(seed));
  94.         }
  95.  
  96.         public void SetSeed(int seed)
  97.         {
  98.             this.Seed = seed;
  99.         }
  100.  
  101.         public static int GenerateHashCode(byte[] data)
  102.         {
  103.             if (data == null || data.Length == 0x0)
  104.             {
  105.                 return 0x0;
  106.             }
  107.             int hashCode = 0x0;
  108.             for (int i = 0; i < data.Length; i++)
  109.             {
  110.                 hashCode = (hashCode << 0x3) | ((hashCode >> 0x1F) ^ data[i]);
  111.             }
  112.             return hashCode;
  113.         }
  114.  
  115.         private static unsafe int GenerateHashCode(string data)
  116.         {
  117.             fixed (char* str = data)
  118.             {
  119.                 int num = 0x15051505;
  120.                 int num2 = num;
  121.                 int* numPtr = (int*)str;
  122.                 for (int i = data.Length; i > 0; i -= 4)
  123.                 {
  124.                     num = ((num << 0x5) + num + (num >> 0x1f)) ^ numPtr[0];
  125.                     if (i <= 2)
  126.                     {
  127.                         break;
  128.                     }
  129.                     num2 = ((num2 << 0x5) + num2 + (num2 >> 0x1f)) ^ numPtr[1];
  130.                     numPtr += 2;
  131.                 }
  132.                 return num + (num2 * 0x5d588b65);
  133.             }
  134.         }
  135.  
  136.         private int NextOffset()
  137.         {
  138.             int locINext = this._inext;
  139.             int locINextp = this._inextp;
  140.             if (++locINext >= 0x38)
  141.             {
  142.                 locINext = 1;
  143.             }
  144.             if (++locINextp >= 0x38)
  145.             {
  146.                 locINextp = 1;
  147.             }
  148.             int retVal = this._seedArray[locINext] - this._seedArray[locINextp];
  149.             if (retVal == int.MaxValue)
  150.             {
  151.                 retVal--;
  152.             }
  153.             if (retVal < 0)
  154.             {
  155.                 retVal += int.MaxValue;
  156.             }
  157.             this._seedArray[locINext] = retVal;
  158.             this._inext = locINext;
  159.             this._inextp = locINextp;
  160.             return retVal;
  161.         }
  162.  
  163.         public byte[] EncryptString(string data)
  164.         {
  165.             byte[] bytes = Encoding.UTF8.GetBytes(data);
  166.             this.TransformBytes(bytes);
  167.             return bytes;
  168.         }
  169.  
  170.         public string DecryptString(byte[] data)
  171.         {
  172.             this.TransformBytes(data);
  173.             return Encoding.UTF8.GetString(data);
  174.         }
  175.  
  176.         public void EncryptFile(string inputPath, string outputPath)
  177.         {
  178.             byte[] inputBytes = File.ReadAllBytes(inputPath);
  179.             this.TransformBytes(inputBytes);
  180.             File.WriteAllBytes(outputPath, inputBytes);
  181.         }
  182.  
  183.         public void DecryptFile(string inputPath, string outputPath)
  184.         {
  185.             byte[] inputBytes = File.ReadAllBytes(inputPath);
  186.             this.TransformBytes(inputBytes);
  187.             File.WriteAllBytes(outputPath, inputBytes);
  188.         }
  189.  
  190.         public void TransformBytes(byte[] bytes)
  191.         {
  192.             for (int i = 0; i < bytes.Length; i++)
  193.             {
  194.                 bytes[i] = this.TransformByte(bytes[i]);
  195.             }
  196.         }
  197.  
  198.         public byte TransformByte(byte value)
  199.         {
  200.             return (byte)(value ^ this.NextOffset());
  201.         }
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement