XOR128.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. public class XOR128
  3. {
  4. public XOR128(int x_)
  5. {
  6. this.x = (ulong)(471283569 + x_);
  7. }
  8. public uint next()
  9. {
  10. ulong num = this.x ^ this.x << 11;
  11. this.x = this.y;
  12. this.y = this.z;
  13. this.z = this.w;
  14. return (uint)(this.w = (this.w ^ this.w >> 19 ^ (num ^ num >> 8)));
  15. }
  16. public static byte[] DECO(byte[] bo, int seed = 555)
  17. {
  18. int num = bo.Length;
  19. byte[] array = new byte[num];
  20. XOR32 xor = new XOR32(311 + bo.Length);
  21. for (int i = 0; i < num - 1; i++)
  22. {
  23. array[i] = (bo[i + 1] ^ (byte)xor.next());
  24. }
  25. XOR128 xor2 = new XOR128(seed + num - 1);
  26. int num2 = num - 1;
  27. for (int j = 0; j < num2; j++)
  28. {
  29. uint num3 = (uint)((ulong)xor2.next() % (ulong)((long)num2));
  30. byte b = array[j];
  31. array[j] = array[(int)((UIntPtr)num3)];
  32. array[(int)((UIntPtr)num3)] = b;
  33. }
  34. XOR32 xor3 = new XOR32(seed + num - 1);
  35. byte[] array2 = new byte[num - 1 - 1];
  36. for (int k = 0; k < array2.Length; k++)
  37. {
  38. array2[k] = (array[k + 1] ^ (byte)xor3.next());
  39. }
  40. bo = null;
  41. return array2;
  42. }
  43. public static byte[] ENCO(byte[] b_, int seed = 555)
  44. {
  45. byte[] array = new byte[b_.Length + 1];
  46. XOR32 xor = new XOR32(seed + array.Length);
  47. array[0] = 1;
  48. for (int i = 0; i < b_.Length; i++)
  49. {
  50. array[i + 1] = (b_[i] ^ (byte)xor.next());
  51. }
  52. XOR128 xor2 = new XOR128(seed + array.Length);
  53. int num = array.Length;
  54. uint[] array2 = new uint[num];
  55. for (int j = 0; j < array.Length; j++)
  56. {
  57. array2[j] = (uint)((ulong)xor2.next() % (ulong)((long)num));
  58. }
  59. for (int k = num - 1; k >= 0; k--)
  60. {
  61. byte b = array[k];
  62. array[k] = array[(int)((UIntPtr)array2[k])];
  63. array[(int)((UIntPtr)array2[k])] = b;
  64. }
  65. byte[] array3 = new byte[array.Length + 1];
  66. XOR32 xor3 = new XOR32(311 + array3.Length);
  67. for (int l = 0; l < array.Length; l++)
  68. {
  69. array3[l + 1] = (array[l] ^ (byte)xor3.next());
  70. }
  71. array3[0] = 1;
  72. return array3;
  73. }
  74. private ulong x;
  75. private ulong y = 362436069UL;
  76. private ulong z = 521288629UL;
  77. private ulong w = 88675123UL;
  78. }