From 7ea2e9e5c2d7725302cb0f8fbb3c1d3f2e0a90a8 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Sun, 2 Nov 2014 19:39:08 +0800 Subject: [PATCH] add rc4-md5 --- shadowsocks-csharp/Encrypt/OpenSSL.cs | 3 +- .../Encrypt/OpensslEncryptor.cs | 30 ++++++++++++++++--- shadowsocks-csharp/shadowsocks-csharp.csproj | 2 +- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/shadowsocks-csharp/Encrypt/OpenSSL.cs b/shadowsocks-csharp/Encrypt/OpenSSL.cs index 53d65ac7..5afa374b 100755 --- a/shadowsocks-csharp/Encrypt/OpenSSL.cs +++ b/shadowsocks-csharp/Encrypt/OpenSSL.cs @@ -39,6 +39,7 @@ namespace shadowsocks_csharp.Encrypt [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public extern static int EVP_CipherUpdate(IntPtr ctx, byte[] outb, out int outl, byte[] inb, int inl); - + [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] + public extern static IntPtr MD5(byte[] d, long n, byte[] md); } } diff --git a/shadowsocks-csharp/Encrypt/OpensslEncryptor.cs b/shadowsocks-csharp/Encrypt/OpensslEncryptor.cs index 5645a934..d8548f4f 100644 --- a/shadowsocks-csharp/Encrypt/OpensslEncryptor.cs +++ b/shadowsocks-csharp/Encrypt/OpensslEncryptor.cs @@ -13,7 +13,8 @@ namespace shadowsocks_csharp.Encrypt {"aes-192-cfb", new int[]{24, 16}}, {"aes-256-cfb", new int[]{32, 16}}, {"bf-cfb", new int[]{16, 8}}, - {"rc4", new int[]{16, 0}}, + {"rc4", new int[]{16, 0}}, + {"rc4-md5", new int[]{16, 16}}, }; static OpensslEncryptor() @@ -86,20 +87,26 @@ namespace shadowsocks_csharp.Encrypt private IntPtr _encryptCtx; private IntPtr _decryptCtx; private IntPtr _cipher; + private string _method; private int keyLen; private int ivLen; private void InitKey(string method, string password) { method = method.ToLower(); + _method = method; string k = method + ":" + password; + if (method == "rc4-md5") + { + method = "rc4"; + } _cipher = OpenSSL.EVP_get_cipherbyname(System.Text.Encoding.UTF8.GetBytes(method)); if (_cipher == null) { throw new Exception("method not found"); } - keyLen = ciphers[method][0]; - ivLen = ciphers[method][1]; + keyLen = ciphers[_method][0]; + ivLen = ciphers[_method][1]; if (CachedKeys.ContainsKey(k)) { _key = CachedKeys[k]; @@ -118,7 +125,22 @@ namespace shadowsocks_csharp.Encrypt { ctx = OpenSSL.EVP_CIPHER_CTX_new(); int enc = isCipher ? 1 : 0; - OpenSSL.EVP_CipherInit_ex(ctx, _cipher, IntPtr.Zero, _key, iv, enc); + byte[] realkey; + IntPtr r = IntPtr.Zero; + if (_method == "rc4-md5") + { + byte[] temp = new byte[keyLen + ivLen]; + realkey = new byte[keyLen]; + Array.Copy(_key, 0, temp, 0, keyLen); + Array.Copy(iv, 0, temp, keyLen, ivLen); + r = OpenSSL.MD5(temp, keyLen + ivLen, null); + Marshal.Copy(r, realkey, 0, keyLen); + } + else + { + realkey = _key; + } + OpenSSL.EVP_CipherInit_ex(ctx, _cipher, IntPtr.Zero, realkey, iv, enc); } #region IDisposable diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index 6c783b95..ab21e895 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -9,7 +9,7 @@ WinExe Properties shadowsocks_csharp - shadowsocks-csharp + Shadowsocks v2.0 512