@@ -10,11 +10,6 @@ namespace shadowsocks_csharp.Encrypt | |||||
return new TableEncryptor(method, password); | return new TableEncryptor(method, password); | ||||
} | } | ||||
if (method.ToLowerInvariant() == "rc4") | |||||
{ | |||||
return new Rc4Encryptor(method, password); | |||||
} | |||||
return new OpensslEncryptor(method, password); | return new OpensslEncryptor(method, password); | ||||
} | } | ||||
} | } | ||||
@@ -0,0 +1,44 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Runtime.InteropServices; | |||||
using System.Text; | |||||
namespace shadowsocks_csharp.Encrypt | |||||
{ | |||||
public class OpenSSL | |||||
{ | |||||
const string DLLNAME = "libeay32"; | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static void OpenSSL_add_all_ciphers(); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static IntPtr EVP_md5(); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static int EVP_BytesToKey(IntPtr type, IntPtr md, IntPtr salt, byte[] data, int datal, int count, byte[] key, byte[] iv); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static int RAND_bytes(byte[] buf, int num); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static IntPtr EVP_get_cipherbyname(byte[] name); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static IntPtr EVP_CIPHER_CTX_new(); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static int EVP_CipherInit_ex(IntPtr ctx, IntPtr type, IntPtr impl, byte[] key, byte[] iv, int enc); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static int EVP_CIPHER_CTX_cleanup(IntPtr a); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static int EVP_CIPHER_CTX_free(IntPtr a); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static int EVP_CipherUpdate(IntPtr ctx, byte[] outb, out int outl, byte[] inb, int inl); | |||||
} | |||||
} |
@@ -1,6 +1,4 @@ | |||||
using OpenSSL.Core; | |||||
using OpenSSL.Crypto; | |||||
using System; | |||||
using System; | |||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.Runtime.InteropServices; | using System.Runtime.InteropServices; | ||||
using System.Text; | using System.Text; | ||||
@@ -9,7 +7,14 @@ namespace shadowsocks_csharp.Encrypt | |||||
{ | { | ||||
public class OpensslEncryptor | public class OpensslEncryptor | ||||
: EncryptorBase, IDisposable | : EncryptorBase, IDisposable | ||||
{ | |||||
{ | |||||
static Dictionary<string, int[]> ciphers = new Dictionary<string, int[]> { | |||||
{"aes-128-cfb", new int[]{16, 16}}, | |||||
{"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}}, | |||||
}; | |||||
public OpensslEncryptor(string method, string password) | public OpensslEncryptor(string method, string password) | ||||
: base(method, password) | : base(method, password) | ||||
{ | { | ||||
@@ -20,23 +25,22 @@ namespace shadowsocks_csharp.Encrypt | |||||
{ | { | ||||
if (_encryptCtx == IntPtr.Zero) | if (_encryptCtx == IntPtr.Zero) | ||||
{ | { | ||||
int ivLen = _cipher.IVLength; | |||||
byte[] iv = new byte[ivLen]; | byte[] iv = new byte[ivLen]; | ||||
Native.RAND_bytes(iv, iv.Length); | |||||
OpenSSL.RAND_bytes(iv, iv.Length); | |||||
InitCipher(ref _encryptCtx, iv, true); | InitCipher(ref _encryptCtx, iv, true); | ||||
int outLen = length + _cipher.BlockSize; | |||||
byte[] cipherText = new byte[outLen]; | |||||
Native.EVP_CipherUpdate(_encryptCtx, cipherText, out outLen, buf, length); | |||||
int outLen = length + ivLen; | |||||
byte[] cipherText = new byte[outLen]; | |||||
OpenSSL.EVP_CipherUpdate(_encryptCtx, cipherText, out outLen, buf, length); | |||||
byte[] result = new byte[outLen + ivLen]; | byte[] result = new byte[outLen + ivLen]; | ||||
Buffer.BlockCopy(iv, 0, result, 0, ivLen); | Buffer.BlockCopy(iv, 0, result, 0, ivLen); | ||||
Buffer.BlockCopy(cipherText, 0, result, ivLen, outLen); | Buffer.BlockCopy(cipherText, 0, result, ivLen, outLen); | ||||
return result; | return result; | ||||
} | } | ||||
else | else | ||||
{ | |||||
int outLen = length + _cipher.BlockSize; | |||||
byte[] cipherText = new byte[outLen]; | |||||
Native.EVP_CipherUpdate(_encryptCtx, cipherText, out outLen, buf, length); | |||||
{ | |||||
int outLen = length + ivLen; | |||||
byte[] cipherText = new byte[outLen]; | |||||
OpenSSL.EVP_CipherUpdate(_encryptCtx, cipherText, out outLen, buf, length); | |||||
byte[] result = new byte[outLen]; | byte[] result = new byte[outLen]; | ||||
Buffer.BlockCopy(cipherText, 0, result, 0, outLen); | Buffer.BlockCopy(cipherText, 0, result, 0, outLen); | ||||
return result; | return result; | ||||
@@ -47,25 +51,24 @@ namespace shadowsocks_csharp.Encrypt | |||||
{ | { | ||||
if (_decryptCtx == IntPtr.Zero) | if (_decryptCtx == IntPtr.Zero) | ||||
{ | { | ||||
int ivLen = _cipher.IVLength; | |||||
byte[] iv = new byte[ivLen]; | byte[] iv = new byte[ivLen]; | ||||
Buffer.BlockCopy(buf, 0, iv, 0, ivLen); | Buffer.BlockCopy(buf, 0, iv, 0, ivLen); | ||||
InitCipher(ref _decryptCtx, iv, false); | |||||
int outLen = length + _cipher.BlockSize; | |||||
InitCipher(ref _decryptCtx, iv, false); | |||||
int outLen = length + ivLen; | |||||
outLen -= ivLen; | outLen -= ivLen; | ||||
byte[] cipherText = new byte[outLen]; | byte[] cipherText = new byte[outLen]; | ||||
byte[] subset = new byte[length - ivLen]; | byte[] subset = new byte[length - ivLen]; | ||||
Buffer.BlockCopy(buf, ivLen, subset, 0, length - ivLen); | |||||
Native.EVP_CipherUpdate(_decryptCtx, cipherText, out outLen, subset, length - ivLen); | |||||
Buffer.BlockCopy(buf, ivLen, subset, 0, length - ivLen); | |||||
OpenSSL.EVP_CipherUpdate(_decryptCtx, cipherText, out outLen, subset, length - ivLen); | |||||
byte[] result = new byte[outLen]; | byte[] result = new byte[outLen]; | ||||
Buffer.BlockCopy(cipherText, 0, result, 0, outLen); | Buffer.BlockCopy(cipherText, 0, result, 0, outLen); | ||||
return result; | return result; | ||||
} | } | ||||
else | else | ||||
{ | |||||
int outLen = length + _cipher.BlockSize; | |||||
byte[] cipherText = new byte[outLen]; | |||||
Native.EVP_CipherUpdate(_decryptCtx, cipherText, out outLen, buf, length); | |||||
{ | |||||
int outLen = length + ivLen; | |||||
byte[] cipherText = new byte[outLen]; | |||||
OpenSSL.EVP_CipherUpdate(_decryptCtx, cipherText, out outLen, buf, length); | |||||
byte[] result = new byte[outLen]; | byte[] result = new byte[outLen]; | ||||
Buffer.BlockCopy(cipherText, 0, result, 0, outLen); | Buffer.BlockCopy(cipherText, 0, result, 0, outLen); | ||||
return result; | return result; | ||||
@@ -73,43 +76,44 @@ namespace shadowsocks_csharp.Encrypt | |||||
} | } | ||||
private static readonly Dictionary<string, byte[]> CachedKeys = new Dictionary<string, byte[]>(); | private static readonly Dictionary<string, byte[]> CachedKeys = new Dictionary<string, byte[]>(); | ||||
private static readonly Dictionary<string, Cipher> CachedCiphers = new Dictionary<string, Cipher>(); | |||||
private byte[] _key; | private byte[] _key; | ||||
private Cipher _cipher; | |||||
private IntPtr _encryptCtx; | |||||
private IntPtr _decryptCtx; | |||||
private IntPtr _encryptCtx; | |||||
private IntPtr _decryptCtx; | |||||
private IntPtr _cipher; | |||||
private int keyLen; | |||||
private int ivLen; | |||||
private void InitKey(string method, string password) | |||||
{ | |||||
string k = method + ":" + password; | |||||
if (CachedKeys.ContainsKey(k)) | |||||
{ | |||||
_key = CachedKeys[k]; | |||||
_cipher = CachedCiphers[k]; | |||||
return; | |||||
} | |||||
_cipher = Cipher.CreateByName(method); | |||||
if (_cipher == null) | |||||
{ | |||||
throw new NullReferenceException(); | |||||
private void InitKey(string method, string password) | |||||
{ | |||||
OpenSSL.OpenSSL_add_all_ciphers(); | |||||
method = method.ToLower(); | |||||
string k = method + ":" + password; | |||||
_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]; | |||||
if (CachedKeys.ContainsKey(k)) | |||||
{ | |||||
_key = CachedKeys[k]; | |||||
} | |||||
else | |||||
{ | |||||
byte[] passbuf = Encoding.UTF8.GetBytes(password); | |||||
_key = new byte[32]; | |||||
byte[] iv = new byte[16]; | |||||
OpenSSL.EVP_BytesToKey(_cipher, OpenSSL.EVP_md5(), IntPtr.Zero, passbuf, passbuf.Length, 1, _key, iv); | |||||
CachedKeys[k] = _key; | |||||
} | } | ||||
byte[] passbuf = Encoding.UTF8.GetBytes(password); | |||||
_key = new byte[_cipher.KeyLength]; | |||||
byte[] iv = new byte[_cipher.IVLength]; | |||||
Native.EVP_BytesToKey(_cipher.Handle, MessageDigest.MD5.Handle, null, passbuf, passbuf.Length, 1, _key, iv); | |||||
CachedKeys[k] = _key; | |||||
CachedCiphers[k] = _cipher; | |||||
} | } | ||||
private void InitCipher(ref IntPtr ctx, byte[] iv, bool isCipher) | private void InitCipher(ref IntPtr ctx, byte[] iv, bool isCipher) | ||||
{ | |||||
ctx = Native.OPENSSL_malloc(Marshal.SizeOf(typeof(CipherContext.EVP_CIPHER_CTX))); | |||||
int enc = isCipher ? 1 : 0; | |||||
Native.EVP_CIPHER_CTX_init(ctx); | |||||
Native.ExpectSuccess(Native.EVP_CipherInit_ex(ctx, _cipher.Handle, IntPtr.Zero, null, null, enc)); | |||||
Native.ExpectSuccess(Native.EVP_CIPHER_CTX_set_key_length(ctx, _key.Length)); | |||||
Native.ExpectSuccess(Native.EVP_CIPHER_CTX_set_padding(ctx, 1)); | |||||
Native.ExpectSuccess(Native.EVP_CipherInit_ex(ctx, _cipher.Handle, IntPtr.Zero, _key, iv, enc)); | |||||
{ | |||||
ctx = OpenSSL.EVP_CIPHER_CTX_new(); | |||||
int enc = isCipher ? 1 : 0; | |||||
OpenSSL.EVP_CipherInit_ex(ctx, _cipher, IntPtr.Zero, _key, iv, enc); | |||||
} | } | ||||
#region IDisposable | #region IDisposable | ||||
@@ -136,15 +140,15 @@ namespace shadowsocks_csharp.Encrypt | |||||
} | } | ||||
if (_encryptCtx != IntPtr.Zero) | if (_encryptCtx != IntPtr.Zero) | ||||
{ | |||||
Native.EVP_CIPHER_CTX_cleanup(_encryptCtx); | |||||
Native.OPENSSL_free(_encryptCtx); | |||||
{ | |||||
OpenSSL.EVP_CIPHER_CTX_cleanup(_encryptCtx); | |||||
OpenSSL.EVP_CIPHER_CTX_free(_encryptCtx); | |||||
_encryptCtx = IntPtr.Zero; | _encryptCtx = IntPtr.Zero; | ||||
} | } | ||||
if (_decryptCtx != IntPtr.Zero) | if (_decryptCtx != IntPtr.Zero) | ||||
{ | |||||
Native.EVP_CIPHER_CTX_cleanup(_decryptCtx); | |||||
Native.OPENSSL_free(_decryptCtx); | |||||
{ | |||||
OpenSSL.EVP_CIPHER_CTX_cleanup(_decryptCtx); | |||||
OpenSSL.EVP_CIPHER_CTX_free(_decryptCtx); | |||||
_decryptCtx = IntPtr.Zero; | _decryptCtx = IntPtr.Zero; | ||||
} | } | ||||
@@ -1,81 +0,0 @@ | |||||
| |||||
namespace shadowsocks_csharp.Encrypt | |||||
{ | |||||
public class Rc4Encryptor | |||||
: EncryptorBase | |||||
{ | |||||
public Rc4Encryptor(string method, string password) | |||||
: base(method, password) | |||||
{ | |||||
byte[] hash = GetPasswordHash(); | |||||
_encryptTable = EncryptInitalize(hash); | |||||
_decryptTable = EncryptInitalize(hash); | |||||
} | |||||
public override byte[] Encrypt(byte[] buf, int length) | |||||
{ | |||||
return EncryptOutput(enc_ctx, _encryptTable, buf, length); | |||||
} | |||||
public override byte[] Decrypt(byte[] buf, int length) | |||||
{ | |||||
return EncryptOutput(dec_ctx, _decryptTable, buf, length); | |||||
} | |||||
private readonly byte[] _encryptTable = new byte[256]; | |||||
private readonly byte[] _decryptTable = new byte[256]; | |||||
private Context enc_ctx = new Context(); | |||||
private Context dec_ctx = new Context(); | |||||
private byte[] EncryptOutput(Context ctx, byte[] s, byte[] data, int length) | |||||
{ | |||||
byte[] result = new byte[length]; | |||||
for (int n = 0; n < length; n++) | |||||
{ | |||||
byte b = data[n]; | |||||
ctx.Index1 = (ctx.Index1 + 1) & 255; | |||||
ctx.Index2 = (ctx.Index2 + s[ctx.Index1]) & 255; | |||||
Swap(s, ctx.Index1, ctx.Index2); | |||||
result[n] = (byte)(b ^ s[(s[ctx.Index1] + s[ctx.Index2]) & 255]); | |||||
} | |||||
return result; | |||||
} | |||||
private byte[] EncryptInitalize(byte[] key) | |||||
{ | |||||
var s = new byte[256]; | |||||
for (int i = 0; i < 256; i++) | |||||
{ | |||||
s[i] = (byte)i; | |||||
} | |||||
for (int i = 0, j = 0; i < 256; i++) | |||||
{ | |||||
j = (j + key[i % key.Length] + s[i]) & 255; | |||||
Swap(s, i, j); | |||||
} | |||||
return s; | |||||
} | |||||
private static void Swap(byte[] s, int i, int j) | |||||
{ | |||||
byte c = s[i]; | |||||
s[i] = s[j]; | |||||
s[j] = c; | |||||
} | |||||
class Context | |||||
{ | |||||
public int Index1; | |||||
public int Index2; | |||||
} | |||||
} | |||||
} |
@@ -1,277 +0,0 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
using System.Security.Cryptography; | |||||
using OpenSSL.Core; | |||||
using OpenSSL.Crypto; | |||||
using System.Runtime.InteropServices; | |||||
namespace shadowsocks_csharp | |||||
{ | |||||
class Encryptor : IDisposable | |||||
{ | |||||
public static string[] encryption_names = new string[] { | |||||
"table", | |||||
"rc4", | |||||
"aes-256-cfb", | |||||
"aes-192-cfb", | |||||
"aes-128-cfb", | |||||
"bf-cfb" | |||||
}; | |||||
public byte[] encryptTable = new byte[256]; | |||||
public byte[] decryptTable = new byte[256]; | |||||
public string method = "table"; | |||||
public string password; | |||||
public byte[] key; | |||||
private RC4 rc4 = null; | |||||
private Cipher cipher = null; | |||||
private IntPtr encryptCTX; | |||||
private IntPtr decryptCTX; | |||||
private static Dictionary<string, byte[]> cachedKeys = new Dictionary<string, byte[]>(); | |||||
private static Dictionary<string, Cipher> cachedCiphers = new Dictionary<string, Cipher>(); | |||||
public void Dispose() | |||||
{ | |||||
if (encryptCTX != IntPtr.Zero) | |||||
{ | |||||
Native.EVP_CIPHER_CTX_cleanup(encryptCTX); | |||||
Native.OPENSSL_free(encryptCTX); | |||||
encryptCTX = IntPtr.Zero; | |||||
} | |||||
if (decryptCTX != IntPtr.Zero) | |||||
{ | |||||
Native.EVP_CIPHER_CTX_cleanup(decryptCTX); | |||||
Native.OPENSSL_free(decryptCTX); | |||||
decryptCTX = IntPtr.Zero; | |||||
} | |||||
} | |||||
~Encryptor() { | |||||
Dispose(); | |||||
} | |||||
private long compare(byte x, byte y, ulong a, int i) | |||||
{ | |||||
return (long)(a % (ulong)(x + i)) - (long)(a % (ulong)(y + i)); | |||||
} | |||||
private byte[] mergeSort(byte[] array, ulong a, int j) | |||||
{ | |||||
if (array.Length == 1) | |||||
return array; | |||||
int middle = array.Length / 2; | |||||
byte[] left = new byte[middle]; | |||||
for (int i = 0; i < middle; i++) | |||||
{ | |||||
left[i] = array[i]; | |||||
} | |||||
byte[] right = new byte[array.Length - middle]; | |||||
for (int i = 0; i < array.Length - middle; i++) | |||||
{ | |||||
right[i] = array[i + middle]; | |||||
} | |||||
left = mergeSort(left, a, j); | |||||
right = mergeSort(right, a, j); | |||||
int leftptr = 0; | |||||
int rightptr = 0; | |||||
byte[] sorted = new byte[array.Length]; | |||||
for (int k = 0; k < array.Length; k++) | |||||
{ | |||||
if (rightptr == right.Length || ((leftptr < left.Length) && (compare(left[leftptr], right[rightptr], a, j) <= 0))) | |||||
{ | |||||
sorted[k] = left[leftptr]; | |||||
leftptr++; | |||||
} | |||||
else if (leftptr == left.Length || ((rightptr < right.Length) && (compare(right[rightptr], left[leftptr], a, j)) <= 0)) | |||||
{ | |||||
sorted[k] = right[rightptr]; | |||||
rightptr++; | |||||
} | |||||
} | |||||
return sorted; | |||||
} | |||||
public Encryptor(string method, string password) | |||||
{ | |||||
MD5 md5 = System.Security.Cryptography.MD5.Create(); | |||||
byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(password); | |||||
byte[] hash = md5.ComputeHash(inputBytes); | |||||
encryptCTX = IntPtr.Zero; | |||||
decryptCTX = IntPtr.Zero; | |||||
this.method = method; | |||||
this.password = password; | |||||
if (method != null && method.ToLowerInvariant().Equals("rc4")) { | |||||
Console.WriteLine("init rc4"); | |||||
rc4 = new RC4(); | |||||
encryptTable = rc4.EncryptInitalize(hash); | |||||
decryptTable = rc4.EncryptInitalize(hash); | |||||
} | |||||
else if (method == "table" || method == "" || method == null) | |||||
{ | |||||
Console.WriteLine("init table"); | |||||
// TODO endian | |||||
var a = BitConverter.ToUInt64(hash, 0); | |||||
for (int i = 0; i < 256; i++) | |||||
{ | |||||
encryptTable[i] = (byte)i; | |||||
} | |||||
for (int i = 1; i < 1024; i++) | |||||
{ | |||||
encryptTable = mergeSort(encryptTable, a, i); | |||||
} | |||||
for (int i = 0; i < 256; i++) | |||||
{ | |||||
decryptTable[encryptTable[i]] = (byte)i; | |||||
} | |||||
} | |||||
else | |||||
{ | |||||
initKey(password, method); | |||||
} | |||||
} | |||||
private void initCipher(ref IntPtr ctx, byte[] iv, bool isCipher) | |||||
{ | |||||
ctx = Native.OPENSSL_malloc(Marshal.SizeOf(typeof(CipherContext.EVP_CIPHER_CTX))); | |||||
int enc = isCipher ? 1 : 0; | |||||
Native.EVP_CIPHER_CTX_init(ctx); | |||||
Native.ExpectSuccess(Native.EVP_CipherInit_ex( | |||||
ctx, this.cipher.Handle, IntPtr.Zero, null, null, enc)); | |||||
Native.ExpectSuccess(Native.EVP_CIPHER_CTX_set_key_length(ctx, key.Length)); | |||||
Native.ExpectSuccess(Native.EVP_CIPHER_CTX_set_padding(ctx, 1)); | |||||
Native.ExpectSuccess(Native.EVP_CipherInit_ex( | |||||
ctx, this.cipher.Handle, IntPtr.Zero, key, iv, enc)); | |||||
} | |||||
private void initKey(string password, string method) | |||||
{ | |||||
string k = method + ":" + password; | |||||
if (cachedKeys.ContainsKey(k)) | |||||
{ | |||||
key = cachedKeys[k]; | |||||
cipher = cachedCiphers[k]; | |||||
return; | |||||
} | |||||
cipher = Cipher.CreateByName(method); | |||||
if (cipher == null) | |||||
{ | |||||
throw new NullReferenceException(); | |||||
} | |||||
byte[] passbuf = System.Text.Encoding.UTF8.GetBytes(password); ; | |||||
key = new byte[cipher.KeyLength]; | |||||
byte[] iv = new byte[cipher.IVLength]; | |||||
Native.EVP_BytesToKey(cipher.Handle, MessageDigest.MD5.Handle, null, passbuf, passbuf.Length, 1, key, iv); | |||||
cachedKeys[k] = key; | |||||
cachedCiphers[k] = cipher; | |||||
} | |||||
private byte[] sslEncrypt(byte[] buf, int length) | |||||
{ | |||||
if (encryptCTX == IntPtr.Zero) | |||||
{ | |||||
int ivLen = cipher.IVLength; | |||||
byte[] iv = new byte[ivLen]; | |||||
Native.RAND_bytes(iv, iv.Length); | |||||
initCipher(ref encryptCTX, iv, true); | |||||
int outLen = length + cipher.BlockSize; | |||||
byte[] cipherText = new byte[outLen]; | |||||
Native.EVP_CipherUpdate(encryptCTX, cipherText, out outLen, buf, length); | |||||
byte[] result = new byte[outLen + ivLen]; | |||||
System.Buffer.BlockCopy(iv, 0, result, 0, ivLen); | |||||
System.Buffer.BlockCopy(cipherText, 0, result, ivLen, outLen); | |||||
return result; | |||||
} | |||||
else | |||||
{ | |||||
int outLen = length + cipher.BlockSize; | |||||
byte[] cipherText = new byte[outLen]; | |||||
Native.EVP_CipherUpdate(encryptCTX, cipherText, out outLen, buf, length); | |||||
byte[] result = new byte[outLen]; | |||||
System.Buffer.BlockCopy(cipherText, 0, result, 0, outLen); | |||||
return result; | |||||
} | |||||
} | |||||
private byte[] sslDecrypt(byte[] buf, int length) | |||||
{ | |||||
if (decryptCTX == IntPtr.Zero) | |||||
{ | |||||
int ivLen = cipher.IVLength; | |||||
byte[] iv = new byte[ivLen]; | |||||
System.Buffer.BlockCopy(buf, 0, iv, 0, ivLen); | |||||
initCipher(ref decryptCTX, iv, false); | |||||
int outLen = length + cipher.BlockSize; | |||||
outLen -= ivLen; | |||||
byte[] cipherText = new byte[outLen]; | |||||
byte[] subset = new byte[length - ivLen]; | |||||
System.Buffer.BlockCopy(buf, ivLen, subset, 0, length - ivLen); | |||||
Native.EVP_CipherUpdate(decryptCTX, cipherText, out outLen, subset, length - ivLen); | |||||
byte[] result = new byte[outLen]; | |||||
System.Buffer.BlockCopy(cipherText, 0, result, 0, outLen); | |||||
return result; | |||||
} | |||||
else | |||||
{ | |||||
int outLen = length + cipher.BlockSize; | |||||
byte[] cipherText = new byte[outLen]; | |||||
Native.EVP_CipherUpdate(decryptCTX, cipherText, out outLen, buf, length); | |||||
byte[] result = new byte[outLen]; | |||||
System.Buffer.BlockCopy(cipherText, 0, result, 0, outLen); | |||||
return result; | |||||
} | |||||
} | |||||
public byte[] byteArrayWith(byte[] buf, int length) | |||||
{ | |||||
if (buf.Length == length) | |||||
{ | |||||
return buf; | |||||
} | |||||
byte[] result = new byte[length]; | |||||
System.Buffer.BlockCopy(buf, 0, result, 0, length); | |||||
return result; | |||||
} | |||||
public byte[] Encrypt(byte[] buf, int length) | |||||
{ | |||||
switch (method) | |||||
{ | |||||
case "table": | |||||
for (int i = 0; i < length; i++) | |||||
buf[i] = encryptTable[buf[i]]; | |||||
return byteArrayWith(buf, length); | |||||
break; | |||||
case "rc4": | |||||
rc4.Encrypt(encryptTable, buf, length); | |||||
return byteArrayWith(buf, length); | |||||
break; | |||||
default: | |||||
return sslEncrypt(buf, length); | |||||
} | |||||
} | |||||
public byte[] Decrypt(byte[] buf, int length) | |||||
{ | |||||
switch (method) | |||||
{ | |||||
case "table": | |||||
for (int i = 0; i < length; i++) | |||||
buf[i] = decryptTable[buf[i]]; | |||||
return byteArrayWith(buf, length); | |||||
break; | |||||
case "rc4": | |||||
rc4.Decrypt(decryptTable, buf, length); | |||||
return byteArrayWith(buf, length); | |||||
break; | |||||
default: | |||||
return sslDecrypt(buf, length); | |||||
} | |||||
} | |||||
} | |||||
} |
@@ -122,12 +122,11 @@ | |||||
this.comboBox1.ItemHeight = 13; | this.comboBox1.ItemHeight = 13; | ||||
this.comboBox1.Items.AddRange(new object[] { | this.comboBox1.Items.AddRange(new object[] { | ||||
"table", | "table", | ||||
"rc4-md5", | |||||
"aes-256-cfb", | "aes-256-cfb", | ||||
"aes-192-cfb", | "aes-192-cfb", | ||||
"aes-128-cfb", | "aes-128-cfb", | ||||
"bf-cfb", | "bf-cfb", | ||||
"cast5-cfb", | |||||
"des-cfb", | |||||
"rc4"}); | "rc4"}); | ||||
this.comboBox1.Location = new System.Drawing.Point(69, 107); | this.comboBox1.Location = new System.Drawing.Point(69, 107); | ||||
this.comboBox1.Name = "comboBox1"; | this.comboBox1.Name = "comboBox1"; | ||||
@@ -10,7 +10,7 @@ using System.Runtime.InteropServices; | |||||
[assembly: AssemblyConfiguration("")] | [assembly: AssemblyConfiguration("")] | ||||
[assembly: AssemblyCompany("")] | [assembly: AssemblyCompany("")] | ||||
[assembly: AssemblyProduct("shadowsocks-csharp")] | [assembly: AssemblyProduct("shadowsocks-csharp")] | ||||
[assembly: AssemblyCopyright("Copyright © 2013")] | |||||
[assembly: AssemblyCopyright("Copyright © 2014")] | |||||
[assembly: AssemblyTrademark("")] | [assembly: AssemblyTrademark("")] | ||||
[assembly: AssemblyCulture("")] | [assembly: AssemblyCulture("")] | ||||
@@ -1,26 +0,0 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
using System.Diagnostics; | |||||
namespace shadowsocks_csharp | |||||
{ | |||||
class Test | |||||
{ | |||||
private static byte[][] target = new byte[][] {new byte[]{60, 53, 84, 138, 217, 94, 88, 23, 39, 242, 219, 35, 12, 157, 165, 181, 255, 143, 83, 247, 162, 16, 31, 209, 190, 171, 115, 65, 38, 41, 21, 245, 236, 46, 121, 62, 166, 233, 44, 154, 153, 145, 230, 49, 128, 216, 173, 29, 241, 119, 64, 229, 194, 103, 131, 110, 26, 197, 218, 59, 204, 56, 27, 34, 141, 221, 149, 239, 192, 195, 24, 155, 170, 183, 11, 254, 213, 37, 137, 226, 75, 203, 55, 19, 72, 248, 22, 129, 33, 175, 178, 10, 198, 71, 77, 36, 113, 167, 48, 2, 117, 140, 142, 66, 199, 232, 243, 32, 123, 54, 51, 82, 57, 177, 87, 251, 150, 196, 133, 5, 253, 130, 8, 184, 14, 152, 231, 3, 186, 159, 76, 89, 228, 205, 156, 96, 163, 146, 18, 91, 132, 85, 80, 109, 172, 176, 105, 13, 50, 235, 127, 0, 189, 95, 98, 136, 250, 200, 108, 179, 211, 214, 106, 168, 78, 79, 74, 210, 30, 73, 201, 151, 208, 114, 101, 174, 92, 52, 120, 240, 15, 169, 220, 182, 81, 224, 43, 185, 40, 99, 180, 17, 212, 158, 42, 90, 9, 191, 45, 6, 25, 4, 222, 67, 126, 1, 116, 124, 206, 69, 61, 7, 68, 97, 202, 63, 244, 20, 28, 58, 93, 134, 104, 144, 227, 147, 102, 118, 135, 148, 47, 238, 86, 112, 122, 70, 107, 215, 100, 139, 223, 225, 164, 237, 111, 125, 207, 160, 187, 246, 234, 161, 188, 193, 249, 252}, | |||||
new byte[]{151, 205, 99, 127, 201, 119, 199, 211, 122, 196, 91, 74, 12, 147, 124, 180, 21, 191, 138, 83, 217, 30, 86, 7, 70, 200, 56, 62, 218, 47, 168, 22, 107, 88, 63, 11, 95, 77, 28, 8, 188, 29, 194, 186, 38, 198, 33, 230, 98, 43, 148, 110, 177, 1, 109, 82, 61, 112, 219, 59, 0, 210, 35, 215, 50, 27, 103, 203, 212, 209, 235, 93, 84, 169, 166, 80, 130, 94, 164, 165, 142, 184, 111, 18, 2, 141, 232, 114, 6, 131, 195, 139, 176, 220, 5, 153, 135, 213, 154, 189, 238, 174, 226, 53, 222, 146, 162, 236, 158, 143, 55, 244, 233, 96, 173, 26, 206, 100, 227, 49, 178, 34, 234, 108, 207, 245, 204, 150, 44, 87, 121, 54, 140, 118, 221, 228, 155, 78, 3, 239, 101, 64, 102, 17, 223, 41, 137, 225, 229, 66, 116, 171, 125, 40, 39, 71, 134, 13, 193, 129, 247, 251, 20, 136, 242, 14, 36, 97, 163, 181, 72, 25, 144, 46, 175, 89, 145, 113, 90, 159, 190, 15, 183, 73, 123, 187, 128, 248, 252, 152, 24, 197, 68, 253, 52, 69, 117, 57, 92, 104, 157, 170, 214, 81, 60, 133, 208, 246, 172, 23, 167, 160, 192, 76, 161, 237, 45, 4, 58, 10, 182, 65, 202, 240, 185, 241, 79, 224, 132, 51, 42, 126, 105, 37, 250, 149, 32, 243, 231, 67, 179, 48, 9, 106, 216, 31, 249, 19, 85, 254, 156, 115, 255, 120, 75, 16}}; | |||||
public static void Test1() | |||||
{ | |||||
Encryptor encryptor = new Encryptor("table", "foobar!"); | |||||
for (int i = 0; i < 256; i++) | |||||
{ | |||||
Debug.Assert(encryptor.encryptTable[i] == target[0][i]); | |||||
Debug.Assert(encryptor.decryptTable[i] == target[1][i]); | |||||
} | |||||
} | |||||
} | |||||
} |
@@ -76,9 +76,6 @@ | |||||
<CodeAnalysisRuleSet>ManagedMinimumRules.ruleset</CodeAnalysisRuleSet> | <CodeAnalysisRuleSet>ManagedMinimumRules.ruleset</CodeAnalysisRuleSet> | ||||
</PropertyGroup> | </PropertyGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<Reference Include="ManagedOpenSsl"> | |||||
<HintPath>..\openssl\openssl-net-0.5\ManagedOpenSsl.dll</HintPath> | |||||
</Reference> | |||||
<Reference Include="System" /> | <Reference Include="System" /> | ||||
<Reference Include="System.Drawing" /> | <Reference Include="System.Drawing" /> | ||||
<Reference Include="System.Windows.Forms" /> | <Reference Include="System.Windows.Forms" /> | ||||
@@ -86,13 +83,12 @@ | |||||
<ItemGroup> | <ItemGroup> | ||||
<Compile Include="Encrypt\EncryptorBase.cs" /> | <Compile Include="Encrypt\EncryptorBase.cs" /> | ||||
<Compile Include="Encrypt\EncryptorFactory.cs" /> | <Compile Include="Encrypt\EncryptorFactory.cs" /> | ||||
<Compile Include="Encrypt\OpenSSL.cs" /> | |||||
<Compile Include="Encrypt\OpensslEncryptor.cs" /> | <Compile Include="Encrypt\OpensslEncryptor.cs" /> | ||||
<Compile Include="Encrypt\Rc4Encryptor.cs" /> | |||||
<Compile Include="Encrypt\TableEncryptor.cs" /> | <Compile Include="Encrypt\TableEncryptor.cs" /> | ||||
<Compile Include="Encrypt\IEncryptor.cs" /> | <Compile Include="Encrypt\IEncryptor.cs" /> | ||||
<Compile Include="RC4.cs" /> | <Compile Include="RC4.cs" /> | ||||
<Compile Include="Config.cs" /> | <Compile Include="Config.cs" /> | ||||
<Compile Include="Encryptor.cs" /> | |||||
<Compile Include="Form1.cs"> | <Compile Include="Form1.cs"> | ||||
<SubType>Form</SubType> | <SubType>Form</SubType> | ||||
</Compile> | </Compile> | ||||
@@ -127,7 +123,6 @@ | |||||
<DesignTimeSharedInput>True</DesignTimeSharedInput> | <DesignTimeSharedInput>True</DesignTimeSharedInput> | ||||
</Compile> | </Compile> | ||||
<Compile Include="simple-json\src\SimpleJson\SimpleJson.cs" /> | <Compile Include="simple-json\src\SimpleJson\SimpleJson.cs" /> | ||||
<Compile Include="Test.cs" /> | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<Content Include="icon144.ico" /> | <Content Include="icon144.ico" /> | ||||