You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Encryptor.cs 4.1 kB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. namespace shadowsocks_csharp
  6. {
  7. class Encryptor
  8. {
  9. public const int TYPE_TABLE = 1;
  10. public const int TYPE_RC4 = 2;
  11. public byte[] encryptTable = new byte[256];
  12. public byte[] decryptTable = new byte[256];
  13. public int method = TYPE_TABLE;
  14. public RC4 rc4 = null;
  15. private long compare(byte x, byte y, ulong a, int i)
  16. {
  17. return (long)(a % (ulong)(x + i)) - (long)(a % (ulong)(y + i));
  18. }
  19. private byte[] mergeSort(byte[] array, ulong a, int j)
  20. {
  21. if (array.Length == 1)
  22. return array;
  23. int middle = array.Length / 2;
  24. byte[] left = new byte[middle];
  25. for (int i = 0; i < middle; i++)
  26. {
  27. left[i] = array[i];
  28. }
  29. byte[] right = new byte[array.Length - middle];
  30. for (int i = 0; i < array.Length - middle; i++)
  31. {
  32. right[i] = array[i + middle];
  33. }
  34. left = mergeSort(left, a, j);
  35. right = mergeSort(right, a, j);
  36. int leftptr = 0;
  37. int rightptr = 0;
  38. byte[] sorted = new byte[array.Length];
  39. for (int k = 0; k < array.Length; k++)
  40. {
  41. if (rightptr == right.Length || ((leftptr < left.Length) && (compare(left[leftptr], right[rightptr], a, j) <= 0)))
  42. {
  43. sorted[k] = left[leftptr];
  44. leftptr++;
  45. }
  46. else if (leftptr == left.Length || ((rightptr < right.Length) && (compare(right[rightptr], left[leftptr], a, j)) <= 0))
  47. {
  48. sorted[k] = right[rightptr];
  49. rightptr++;
  50. }
  51. }
  52. return sorted;
  53. }
  54. public Encryptor(string method, string password)
  55. {
  56. MD5 md5 = System.Security.Cryptography.MD5.Create();
  57. byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(password);
  58. byte[] hash = md5.ComputeHash(inputBytes);
  59. if (method != null && method.ToLowerInvariant().Equals("rc4")) {
  60. Console.WriteLine("init rc4");
  61. this.method = TYPE_RC4;
  62. rc4 = new RC4();
  63. encryptTable = rc4.EncryptInitalize(hash);
  64. decryptTable = rc4.EncryptInitalize(hash);
  65. } else {
  66. Console.WriteLine("init table");
  67. this.method = TYPE_TABLE;
  68. // TODO endian
  69. var a = BitConverter.ToUInt64(hash, 0);
  70. for (int i = 0; i < 256; i++)
  71. {
  72. encryptTable[i] = (byte)i;
  73. }
  74. for (int i = 1; i < 1024; i++)
  75. {
  76. encryptTable = mergeSort(encryptTable, a, i);
  77. }
  78. for (int i = 0; i < 256; i++)
  79. {
  80. decryptTable[encryptTable[i]] = (byte)i;
  81. }
  82. }
  83. }
  84. public void Encrypt(byte[] buf, int length)
  85. {
  86. switch (method)
  87. {
  88. case TYPE_TABLE:
  89. for (int i = 0; i < length; i++)
  90. buf[i] = encryptTable[buf[i]];
  91. break;
  92. case TYPE_RC4:
  93. rc4.Encrypt(encryptTable, buf, length);
  94. break;
  95. }
  96. }
  97. public void Decrypt(byte[] buf, int length)
  98. {
  99. switch (method)
  100. {
  101. case TYPE_TABLE:
  102. for (int i = 0; i < length; i++)
  103. buf[i] = decryptTable[buf[i]];
  104. break;
  105. case TYPE_RC4:
  106. rc4.Decrypt(decryptTable, buf, length);
  107. break;
  108. }
  109. }
  110. }
  111. }

No Description