Commit bfa0f1cf authored by Max Lv's avatar Max Lv

working on rc4 cipher

parent 57f6e87f
No preview for this file type
#include "encrypt.h"
#include "android.h"
#include <openssl/md5.h>
#include <endian.h>
static int random_compare(const void *_x, const void *_y) {
uint32_t i = _i;
uint64_t a = _a;
......@@ -115,30 +112,42 @@ static void mergesort(uint8_t array[], int length)
}
void encrypt(char *buf, int len) {
char *end = buf + len;
while (buf < end) {
*buf = (char)encrypt_table[(uint8_t)*buf];
buf++;
if (_method == RC4_ENC) {
unsigned char mybuf[BUF_SIZE];
RC4(&rc4_key, len, (unsigned char *) buf, mybuf);
memcpy(buf, mybuf, len);
} else {
char *end = buf + len;
while (buf < end) {
*buf = (char)encrypt_table[(uint8_t)*buf];
buf++;
}
}
}
void decrypt(char *buf, int len) {
char *end = buf + len;
while (buf < end) {
*buf = (char)decrypt_table[(uint8_t)*buf];
buf++;
if (_method == RC4_ENC) {
unsigned char mybuf[BUF_SIZE];
RC4(&rc4_key, len, (unsigned char *) buf, mybuf);
memcpy(buf, mybuf, len);
} else {
char *end = buf + len;
while (buf < end) {
*buf = (char)decrypt_table[(uint8_t)*buf];
buf++;
}
}
}
int send_encrypt(int sock, char *buf, int len, int flags) {
char mybuf[4096];
char mybuf[BUF_SIZE];
memcpy(mybuf, buf, len);
encrypt(mybuf, len);
return send(sock, mybuf, len, flags);
}
int recv_decrypt(int sock, char *buf, int len, int flags) {
char mybuf[4096];
char mybuf[BUF_SIZE];
int result = recv(sock, mybuf, len, flags);
memcpy(buf, mybuf, len);
decrypt(buf, len);
......@@ -146,20 +155,24 @@ int recv_decrypt(int sock, char *buf, int len, int flags) {
}
void get_table(const char* key) {
uint8_t *table = encrypt_table;
uint8_t *tmp_hash = MD5((const uint8_t*)key, strlen(key), NULL);
_a = htole64(*(uint64_t *)tmp_hash);
uint32_t i;
for(i = 0; i < 256; ++i) {
table[i] = i;
}
for(i = 1; i < 1024; ++i) {
_i = i;
mergesort(table, 256);
}
for(i = 0; i < 256; ++i) {
// gen decrypt table from encrypt table
decrypt_table[encrypt_table[i]] = i;
if (_method == RC4_ENC) {
RC4_set_key(&rc4_key, strlen(key), (unsigned char *) key);
} else {
uint8_t *table = encrypt_table;
uint8_t *tmp_hash = MD5((const uint8_t*)key, strlen(key), NULL);
_a = htole64(*(uint64_t *)tmp_hash);
uint32_t i;
for(i = 0; i < 256; ++i) {
table[i] = i;
}
for(i = 1; i < 1024; ++i) {
_i = i;
mergesort(table, 256);
}
for(i = 0; i < 256; ++i) {
// gen decrypt table from encrypt table
decrypt_table[encrypt_table[i]] = i;
}
}
}
......@@ -4,9 +4,19 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <openssl/md5.h>
#include <openssl/rc4.h>
#include <endian.h>
#define BUF_SIZE 4096
#define TABLE_ENC 0
#define RC4_ENC 1
unsigned char encrypt_table[256];
unsigned char decrypt_table[256];
RC4_KEY rc4_key;
void get_table(const char* key);
void encrypt(char *buf, int len);
void decrypt(char *buf, int len);
......@@ -15,4 +25,5 @@ int recv_decrypt(int sock, char *buf, int len, int flags);
unsigned int _i;
unsigned long long _a;
int _method;
......@@ -21,7 +21,6 @@
#include "local.h"
#include "socks5.h"
#include "encrypt.h"
#include "android.h"
#define min(a,b) (((a)<(b))?(a):(b))
......@@ -567,12 +566,13 @@ int main (int argc, char **argv)
char *port = NULL;
char *key = NULL;
char* timeout = "10";
char* method = NULL;
int c;
int f_flags = 0;
opterr = 0;
while ((c = getopt (argc, argv, "fs:p:l:k:t:")) != -1) {
while ((c = getopt (argc, argv, "fs:p:l:k:t:m:")) != -1) {
switch (c) {
case 's':
server = optarg;
......@@ -592,6 +592,9 @@ int main (int argc, char **argv)
case 't':
timeout = optarg;
break;
case 'm':
method = optarg;
break;
}
}
......@@ -600,6 +603,12 @@ int main (int argc, char **argv)
exit(EXIT_FAILURE);
}
if (method != NULL) {
if (strcmp(method, "rc4") == 0) {
_method = RC4_ENC;
}
}
if (f_flags) {
/* Our process ID and Session ID */
......@@ -653,7 +662,7 @@ int main (int argc, char **argv)
_remote_port = strdup(remote_port);
_timeout = atoi(timeout);
LOGD("calculating ciphers");
LOGD("calculating ciphers %d", _method);
get_table(key);
int listenfd;
......
......@@ -2,7 +2,7 @@
#include <ev.h>
#define BUF_SIZE 4096
#include "encrypt.h"
struct listen_ctx {
ev_io io;
......
No preview for this file type
......@@ -61,10 +61,15 @@
<string name="copy_rights">Shadowsocks is an open source socks proxy.\n\nYou can find more details here:\ngithub.com/shadowsocks
</string>
<string name="about">About</string>
<string name="proxy_type">Proxy Type</string>
<array name="proxy_type_entry">
<item>GAE</item>
<item>PaaS</item>
<string name="enc_method">Encrypt Method</string>
<array name="enc_method_entry">
<item>Table</item>
<item>RC4</item>
</array>
<array name="enc_method_value">
<item>table</item>
<item>rc4</item>
</array>
<string-array name="gfw_list">
......
......@@ -39,6 +39,14 @@
android:summary="@string/sitekey_summary"
android:title="@string/sitekey">
</EditTextPreference>
<ListPreference
android:dependency="isRunning"
android:defaultValue="table"
android:key="encMethod"
android:entries="@array/enc_method_entry"
android:entryValues="@array/enc_method_value"
android:title="@string/enc_method">
</ListPreference>
</PreferenceCategory>
<PreferenceCategory android:title="@string/fearute_cat">
<CheckBoxPreference
......
......@@ -98,6 +98,7 @@ public class Shadowsocks extends PreferenceActivity implements
private EditTextPreference portText;
private EditTextPreference remotePortText;
private EditTextPreference sitekeyText;
private ListPreference encMethodText;
private Preference proxyedApps;
private CheckBoxPreference isBypassAppsCheck;
private CheckBoxPreference isRunningCheck;
......@@ -169,6 +170,7 @@ public class Shadowsocks extends PreferenceActivity implements
portText = (EditTextPreference) findPreference("port");
remotePortText = (EditTextPreference) findPreference("remotePort");
sitekeyText = (EditTextPreference) findPreference("sitekey");
encMethodText = (ListPreference) findPreference("encMethod");
proxyedApps = findPreference("proxyedApps");
isBypassAppsCheck = (CheckBoxPreference) findPreference("isBypassApps");
......@@ -343,6 +345,9 @@ public class Shadowsocks extends PreferenceActivity implements
if (!settings.getString("sitekey", "").equals(""))
sitekeyText.setSummary(settings.getString("sitekey", ""));
if (!settings.getString("encMethod", "").equals(""))
encMethodText.setSummary(settings.getString("encMethod", ""));
if (!settings.getString("port", "").equals(""))
portText.setSummary(settings.getString("port",
getString(R.string.port_summary)));
......@@ -416,6 +421,9 @@ public class Shadowsocks extends PreferenceActivity implements
sitekeyText.setSummary(getString(R.string.sitekey_summary));
else
sitekeyText.setSummary(settings.getString("sitekey", ""));
else if (key.equals("encMethod"))
if (!settings.getString("encMethod", "").equals(""))
encMethodText.setSummary(settings.getString("encMethod", ""));
else if (key.equals("proxy"))
if (settings.getString("proxy", "").equals("")) {
proxyText.setSummary(getString(R.string.proxy_summary));
......
......@@ -164,6 +164,7 @@ public class ShadowsocksService extends Service {
private boolean isGFWList = false;
private boolean isBypassApps = false;
private boolean isDNSProxy = false;
private String encMethod;
private ProxyedApp apps[];
private Method mSetForeground;
private Method mStartForeground;
......@@ -205,8 +206,8 @@ public class ShadowsocksService extends Service {
@Override
public void run() {
final String cmd = String.format(BASE +
"shadowsocks -s \"%s\" -p \"%d\" -l \"%d\" -k \"%s\"",
appHost, remotePort, port, sitekey);
"shadowsocks -s \"%s\" -p \"%d\" -l \"%d\" -k \"%s\" -m \"%s\"",
appHost, remotePort, port, sitekey, encMethod);
Node.exec(cmd);
}
}.start();
......@@ -238,6 +239,7 @@ public class ShadowsocksService extends Service {
appHost = settings.getString("proxy", "127.0.0.1");
sitekey = settings.getString("sitekey", "default");
encMethod = settings.getString("encMethod", "table");
try {
remotePort = Integer.valueOf(settings.getString("remotePort", "1984"));
} catch (NumberFormatException ex) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment