Skip to content

Commit

Permalink
add rc4 encryptor
Browse files Browse the repository at this point in the history
  • Loading branch information
madeye committed May 10, 2013
1 parent bb31a1b commit bea8e54
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 89 deletions.
5 changes: 4 additions & 1 deletion shadowsocks-csharp/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Config
public int server_port;
public int local_port;
public string password;
public string method;

public bool isDefault;

Expand Down Expand Up @@ -49,6 +50,7 @@ public static Config Load()
server_port = 8388,
local_port = 1081,
password = "barfoo!",
method = "table",
isDefault = true
};
}
Expand All @@ -65,7 +67,8 @@ public static void Save(Config config)
server = config.server,
server_port = config.server_port,
local_port = config.local_port,
password = config.password
password = config.password,
method = config.method
});
sw.Write(jsonString);
sw.Flush();
Expand Down
67 changes: 47 additions & 20 deletions shadowsocks-csharp/Encryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ namespace shadowsocks_csharp
{
class Encryptor
{
public const int TYPE_TABLE = 1;
public const int TYPE_RC4 = 2;

public byte[] encryptTable = new byte[256];
public byte[] decryptTable = new byte[256];
public int method = TYPE_TABLE;
public RC4 rc4 = null;

private long compare(byte x, byte y, ulong a, int i)
{
Expand Down Expand Up @@ -53,43 +58,65 @@ private byte[] mergeSort(byte[] array, ulong a, int j)
return sorted;
}

public Encryptor(string password)
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);

// 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;
if (method != null && method.ToLowerInvariant().Equals("rc4")) {
Console.WriteLine("init rc4");
this.method = TYPE_RC4;

rc4 = new RC4();
encryptTable = rc4.EncryptInitalize(hash);
decryptTable = rc4.EncryptInitalize(hash);
} else {
Console.WriteLine("init table");
this.method = TYPE_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;
}
}
}

public void Encrypt(byte[] buf, int length)
{
for (int i = 0; i < length; i++)
switch (method)
{
buf[i] = encryptTable[buf[i]];
case TYPE_TABLE:
for (int i = 0; i < length; i++)
buf[i] = encryptTable[buf[i]];
break;
case TYPE_RC4:
rc4.Encrypt(encryptTable, buf, length);
break;
}
}
public void Decrypt(byte[] buf, int length)
{
for (int i = 0; i < length; i++)
switch (method)
{
buf[i] = decryptTable[buf[i]];
case TYPE_TABLE:
for (int i = 0; i < length; i++)
buf[i] = decryptTable[buf[i]];
break;
case TYPE_RC4:
rc4.Decrypt(decryptTable, buf, length);
break;
}
}


}
}
Loading

0 comments on commit bea8e54

Please sign in to comment.