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.

StreamRc4NativeEncryptor.cs 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. namespace Shadowsocks.Encryption.Stream
  5. {
  6. public class StreamRc4NativeEncryptor : StreamEncryptor
  7. {
  8. byte[] realkey = new byte[256];
  9. byte[] sbox = new byte[256];
  10. public StreamRc4NativeEncryptor(string method, string password) : base(method, password)
  11. {
  12. }
  13. protected override void InitCipher(byte[] iv, bool isEncrypt)
  14. {
  15. base.InitCipher(iv, isEncrypt);
  16. // rc4-md5 is rc4 with md5 based session key
  17. if (cipherFamily == CipherFamily.Rc4Md5)
  18. {
  19. byte[] temp = new byte[keyLen + ivLen];
  20. Array.Copy(key, 0, temp, 0, keyLen);
  21. Array.Copy(iv, 0, temp, keyLen, ivLen);
  22. realkey = CryptoUtils.MD5(temp);
  23. }
  24. else
  25. {
  26. realkey = key;
  27. }
  28. sbox = SBox(realkey);
  29. }
  30. protected override int CipherEncrypt(ReadOnlySpan<byte> plain, Span<byte> cipher)
  31. {
  32. return CipherUpdate(plain, cipher);
  33. }
  34. protected override int CipherDecrypt(Span<byte> plain, ReadOnlySpan<byte> cipher)
  35. {
  36. return CipherUpdate(cipher, plain);
  37. }
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. private int CipherUpdate(ReadOnlySpan<byte> i, Span<byte> o)
  40. {
  41. // don't know why we need third array, but it works...
  42. Span<byte> t = new byte[i.Length];
  43. i.CopyTo(t);
  44. RC4(ctx, sbox, t, t.Length);
  45. t.CopyTo(o);
  46. return t.Length;
  47. }
  48. #region Cipher Info
  49. private static readonly Dictionary<string, CipherInfo> _ciphers = new Dictionary<string, CipherInfo>
  50. {
  51. // original RC4 doesn't use IV
  52. { "rc4", new CipherInfo("rc4", 16, 0, CipherFamily.Rc4) },
  53. { "rc4-md5", new CipherInfo("rc4-md5", 16, 16, CipherFamily.Rc4Md5) },
  54. };
  55. public static Dictionary<string, CipherInfo> SupportedCiphers()
  56. {
  57. return _ciphers;
  58. }
  59. protected override Dictionary<string, CipherInfo> GetCiphers()
  60. {
  61. return _ciphers;
  62. }
  63. #endregion
  64. #region RC4
  65. class Context
  66. {
  67. public int index1 = 0;
  68. public int index2 = 0;
  69. }
  70. private readonly Context ctx = new Context();
  71. private byte[] SBox(byte[] key)
  72. {
  73. byte[] s = new byte[256];
  74. for (int i = 0; i < 256; i++)
  75. {
  76. s[i] = (byte)i;
  77. }
  78. for (int i = 0, j = 0; i < 256; i++)
  79. {
  80. j = (j + key[i % key.Length] + s[i]) & 255;
  81. Swap(s, i, j);
  82. }
  83. return s;
  84. }
  85. [MethodImpl(MethodImplOptions.AggressiveOptimization)]
  86. private void RC4(Context ctx, Span<byte> s, Span<byte> data, int length)
  87. {
  88. for (int n = 0; n < length; n++)
  89. {
  90. byte b = data[n];
  91. ctx.index1 = (ctx.index1 + 1) & 255;
  92. ctx.index2 = (ctx.index2 + s[ctx.index1]) & 255;
  93. Swap(s, ctx.index1, ctx.index2);
  94. data[n] = (byte)(b ^ s[(s[ctx.index1] + s[ctx.index2]) & 255]);
  95. }
  96. }
  97. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  98. private static void Swap(Span<byte> s, int i, int j)
  99. {
  100. byte c = s[i];
  101. s[i] = s[j];
  102. s[j] = c;
  103. }
  104. #endregion
  105. }
  106. }