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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Shadowsocks.Encryption.Stream
  8. {
  9. public class StreamTableNativeEncryptor : StreamEncryptor
  10. {
  11. // table mode use special way to generate key
  12. readonly string _password;
  13. public StreamTableNativeEncryptor(string method, string password) : base(method, password)
  14. {
  15. _password = password;
  16. }
  17. protected override void initCipher(byte[] iv, bool isEncrypt)
  18. {
  19. // another cipher is plain, needn't a table
  20. if (cipherFamily != CipherFamily.Table) return;
  21. ulong a = BitConverter.ToUInt64(CryptoUtils.MD5(Encoding.UTF8.GetBytes(_password)), 0);
  22. for (int i = 0; i < 256; i++)
  23. {
  24. _encryptTable[i] = (byte)i;
  25. }
  26. Span<byte> t = _encryptTable;
  27. // copy array 1024 times? excuse me?
  28. for (int i = 1; i < 1024; i++)
  29. {
  30. t = MergeSort(t, a, i);
  31. }
  32. _encryptTable = t.ToArray();
  33. for (int i = 0; i < 256; i++)
  34. {
  35. _decryptTable[_encryptTable[i]] = (byte)i;
  36. }
  37. }
  38. protected override void cipherUpdate(bool isEncrypt, int length, byte[] buf, byte[] outbuf)
  39. {
  40. if (cipherFamily == CipherFamily.Table)
  41. {
  42. var table = isEncrypt ? _encryptTable : _decryptTable;
  43. for (int i = 0; i < length; i++)
  44. {
  45. outbuf[i] = table[buf[i]];
  46. }
  47. }
  48. else if (cipherFamily == CipherFamily.Plain)
  49. {
  50. Array.Copy(buf, outbuf, length);
  51. }
  52. }
  53. protected override int CipherDecrypt(Span<byte> plain, Span<byte> cipher)
  54. {
  55. if (cipherFamily == CipherFamily.Plain)
  56. {
  57. cipher.CopyTo(plain);
  58. return cipher.Length;
  59. }
  60. for (int i = 0; i < cipher.Length; i++)
  61. {
  62. plain[i] = _decryptTable[cipher[i]];
  63. }
  64. return cipher.Length;
  65. }
  66. protected override int CipherEncrypt(Span<byte> plain, Span<byte> cipher)
  67. {
  68. if (cipherFamily == CipherFamily.Plain)
  69. {
  70. plain.CopyTo(cipher);
  71. return plain.Length;
  72. }
  73. for (int i = 0; i < plain.Length; i++)
  74. {
  75. cipher[i] = _decryptTable[plain[i]];
  76. }
  77. return plain.Length;
  78. }
  79. #region Cipher Info
  80. private static readonly Dictionary<string, CipherInfo> _ciphers = new Dictionary<string, CipherInfo>
  81. {
  82. {"plain", new CipherInfo("plain", 0, 0, CipherFamily.Plain) },
  83. {"table", new CipherInfo("table", 0, 0, CipherFamily.Table) },
  84. };
  85. public static Dictionary<string, CipherInfo> SupportedCiphers()
  86. {
  87. return _ciphers;
  88. }
  89. protected override Dictionary<string, CipherInfo> getCiphers()
  90. {
  91. return _ciphers;
  92. }
  93. #endregion
  94. #region Table
  95. private byte[] _encryptTable = new byte[256];
  96. private byte[] _decryptTable = new byte[256];
  97. private byte[] _tmp = new byte[256];
  98. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  99. private static long Compare(byte x, byte y, ulong a, int i)
  100. {
  101. return (long)(a % (ulong)(x + i)) - (long)(a % (ulong)(y + i));
  102. }
  103. byte[] buf = new byte[1024];
  104. private Span<byte> MergeSort(Span<byte> array, ulong a, int j)
  105. {
  106. if (array.Length == 1)
  107. {
  108. return array;
  109. }
  110. int middle = array.Length / 2;
  111. Span<byte> left = MergeSort(array.Slice(0, middle), a, j);;
  112. Span<byte> right = MergeSort(array.Slice(middle), a, j);
  113. int leftptr = 0;
  114. int rightptr = 0;
  115. // why a new array?
  116. Span<byte> sorted = new byte[array.Length];// buf.AsSpan().Slice(0,array.Length); // // _tmp;
  117. for (int k = 0; k < array.Length; k++)
  118. {
  119. if (rightptr == right.Length || ((leftptr < left.Length) && (Compare(left[leftptr], right[rightptr], a, j) <= 0)))
  120. {
  121. sorted[k] = left[leftptr];
  122. leftptr++;
  123. }
  124. else if (leftptr == left.Length || ((rightptr < right.Length) && (Compare(right[rightptr], left[leftptr], a, j)) <= 0))
  125. {
  126. sorted[k] = right[rightptr];
  127. rightptr++;
  128. }
  129. }
  130. return sorted;
  131. }
  132. #endregion
  133. }
  134. }