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.

StreamTableNativeEncryptor.cs 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Shadowsocks.Encryption.Stream
  7. {
  8. public class StreamTableNativeEncryptor : StreamEncryptor
  9. {
  10. const int Plain = 0;
  11. const int Table = 1;
  12. string _password;
  13. public StreamTableNativeEncryptor(string method, string password) : base(method, password)
  14. {
  15. _password = password;
  16. }
  17. public override void Dispose()
  18. {
  19. return;
  20. }
  21. protected override void initCipher(byte[] iv, bool isEncrypt)
  22. {
  23. base.initCipher(iv, isEncrypt);
  24. if (_cipher == Table)
  25. {
  26. ulong a = BitConverter.ToUInt64(CryptoUtils.MD5(Encoding.UTF8.GetBytes(_password)), 0);
  27. for (int i = 0; i < 256; i++)
  28. {
  29. _encryptTable[i] = (byte)i;
  30. }
  31. for (int i = 1; i < 1024; i++)
  32. {
  33. _encryptTable = MergeSort(_encryptTable, a, i);
  34. }
  35. for (int i = 0; i < 256; i++)
  36. {
  37. _decryptTable[_encryptTable[i]] = (byte)i;
  38. }
  39. }
  40. }
  41. protected override void cipherUpdate(bool isEncrypt, int length, byte[] buf, byte[] outbuf)
  42. {
  43. if (_cipher == Table)
  44. {
  45. var table = isEncrypt ? _encryptTable : _decryptTable;
  46. for (int i = 0; i < length; i++)
  47. {
  48. outbuf[i] = table[buf[i]];
  49. }
  50. }
  51. else if (_cipher == Plain)
  52. {
  53. Array.Copy(buf, outbuf, length);
  54. }
  55. }
  56. private static readonly Dictionary<string, EncryptorInfo> _ciphers = new Dictionary<string, EncryptorInfo>
  57. {
  58. {"plain", new EncryptorInfo("PLAIN", 0, 0, Plain) },
  59. {"table", new EncryptorInfo("TABLE", 0, 0, Table) },
  60. };
  61. public static IEnumerable<string> SupportedCiphers()
  62. {
  63. return _ciphers.Keys;
  64. }
  65. protected override Dictionary<string, EncryptorInfo> getCiphers()
  66. {
  67. return _ciphers;
  68. }
  69. #region Table
  70. private byte[] _encryptTable = new byte[256];
  71. private byte[] _decryptTable = new byte[256];
  72. private static long Compare(byte x, byte y, ulong a, int i)
  73. {
  74. return (long)(a % (ulong)(x + i)) - (long)(a % (ulong)(y + i));
  75. }
  76. private byte[] MergeSort(byte[] array, ulong a, int j)
  77. {
  78. if (array.Length == 1)
  79. {
  80. return array;
  81. }
  82. int middle = array.Length / 2;
  83. byte[] left = new byte[middle];
  84. for (int i = 0; i < middle; i++)
  85. {
  86. left[i] = array[i];
  87. }
  88. byte[] right = new byte[array.Length - middle];
  89. for (int i = 0; i < array.Length - middle; i++)
  90. {
  91. right[i] = array[i + middle];
  92. }
  93. left = MergeSort(left, a, j);
  94. right = MergeSort(right, a, j);
  95. int leftptr = 0;
  96. int rightptr = 0;
  97. byte[] sorted = new byte[array.Length];
  98. for (int k = 0; k < array.Length; k++)
  99. {
  100. if (rightptr == right.Length || ((leftptr < left.Length) && (Compare(left[leftptr], right[rightptr], a, j) <= 0)))
  101. {
  102. sorted[k] = left[leftptr];
  103. leftptr++;
  104. }
  105. else if (leftptr == left.Length || ((rightptr < right.Length) && (Compare(right[rightptr], left[leftptr], a, j)) <= 0))
  106. {
  107. sorted[k] = right[rightptr];
  108. rightptr++;
  109. }
  110. }
  111. return sorted;
  112. }
  113. #endregion
  114. }
  115. }