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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. string _password;
  11. public StreamTableNativeEncryptor(string method, string password) : base(method, password)
  12. {
  13. _password = password;
  14. }
  15. protected override void initCipher(byte[] iv, bool isEncrypt)
  16. {
  17. base.initCipher(iv, isEncrypt);
  18. if (_cipher == CipherFamily.Table)
  19. {
  20. ulong a = BitConverter.ToUInt64(CryptoUtils.MD5(Encoding.UTF8.GetBytes(_password)), 0);
  21. for (int i = 0; i < 256; i++)
  22. {
  23. _encryptTable[i] = (byte)i;
  24. }
  25. for (int i = 1; i < 1024; i++)
  26. {
  27. _encryptTable = MergeSort(_encryptTable, a, i);
  28. }
  29. for (int i = 0; i < 256; i++)
  30. {
  31. _decryptTable[_encryptTable[i]] = (byte)i;
  32. }
  33. }
  34. }
  35. protected override void cipherUpdate(bool isEncrypt, int length, byte[] buf, byte[] outbuf)
  36. {
  37. if (_cipher == CipherFamily.Table)
  38. {
  39. var table = isEncrypt ? _encryptTable : _decryptTable;
  40. for (int i = 0; i < length; i++)
  41. {
  42. outbuf[i] = table[buf[i]];
  43. }
  44. }
  45. else if (_cipher == CipherFamily.Plain)
  46. {
  47. Array.Copy(buf, outbuf, length);
  48. }
  49. }
  50. protected override int CipherDecrypt(Span<byte> plain, Span<byte> cipher)
  51. {
  52. if (_cipher == CipherFamily.Plain)
  53. {
  54. cipher.CopyTo(plain);
  55. return cipher.Length;
  56. }
  57. for (int i = 0; i < cipher.Length; i++)
  58. {
  59. plain[i] = _decryptTable[cipher[i]];
  60. }
  61. return cipher.Length;
  62. }
  63. protected override int CipherEncrypt(Span<byte> plain, Span<byte> cipher)
  64. {
  65. if (_cipher == CipherFamily.Plain)
  66. {
  67. plain.CopyTo(cipher);
  68. return plain.Length;
  69. }
  70. for (int i = 0; i < plain.Length; i++)
  71. {
  72. cipher[i] = _decryptTable[plain[i]];
  73. }
  74. return plain.Length;
  75. }
  76. #region Cipher Info
  77. private static readonly Dictionary<string, CipherInfo> _ciphers = new Dictionary<string, CipherInfo>
  78. {
  79. {"plain", new CipherInfo("plain", 0, 0, CipherFamily.Plain) },
  80. {"table", new CipherInfo("table", 0, 0, CipherFamily.Table) },
  81. };
  82. public static Dictionary<string, CipherInfo> SupportedCiphers()
  83. {
  84. return _ciphers;
  85. }
  86. protected override Dictionary<string, CipherInfo> getCiphers()
  87. {
  88. return _ciphers;
  89. }
  90. #endregion
  91. #region Table
  92. private byte[] _encryptTable = new byte[256];
  93. private byte[] _decryptTable = new byte[256];
  94. private static long Compare(byte x, byte y, ulong a, int i)
  95. {
  96. return (long)(a % (ulong)(x + i)) - (long)(a % (ulong)(y + i));
  97. }
  98. private byte[] MergeSort(byte[] array, ulong a, int j)
  99. {
  100. if (array.Length == 1)
  101. {
  102. return array;
  103. }
  104. int middle = array.Length / 2;
  105. byte[] left = new byte[middle];
  106. for (int i = 0; i < middle; i++)
  107. {
  108. left[i] = array[i];
  109. }
  110. byte[] right = new byte[array.Length - middle];
  111. for (int i = 0; i < array.Length - middle; i++)
  112. {
  113. right[i] = array[i + middle];
  114. }
  115. left = MergeSort(left, a, j);
  116. right = MergeSort(right, a, j);
  117. int leftptr = 0;
  118. int rightptr = 0;
  119. byte[] sorted = new byte[array.Length];
  120. for (int k = 0; k < array.Length; k++)
  121. {
  122. if (rightptr == right.Length || ((leftptr < left.Length) && (Compare(left[leftptr], right[rightptr], a, j) <= 0)))
  123. {
  124. sorted[k] = left[leftptr];
  125. leftptr++;
  126. }
  127. else if (leftptr == left.Length || ((rightptr < right.Length) && (Compare(right[rightptr], left[leftptr], a, j)) <= 0))
  128. {
  129. sorted[k] = right[rightptr];
  130. rightptr++;
  131. }
  132. }
  133. return sorted;
  134. }
  135. #endregion
  136. }
  137. }