Commit b11f386b authored by Max Lv's avatar Max Lv

working on rc4

parent bfa0f1cf
No preview for this file type
......@@ -113,8 +113,9 @@ static void mergesort(uint8_t array[], int length)
void encrypt(char *buf, int len) {
if (_method == RC4_ENC) {
int outlen;
unsigned char mybuf[BUF_SIZE];
RC4(&rc4_key, len, (unsigned char *) buf, mybuf);
EVP_CipherUpdate(&ctx, mybuf, &outlen, (unsigned char*)buf, len);
memcpy(buf, mybuf, len);
} else {
char *end = buf + len;
......@@ -127,8 +128,9 @@ void encrypt(char *buf, int len) {
void decrypt(char *buf, int len) {
if (_method == RC4_ENC) {
int outlen;
unsigned char mybuf[BUF_SIZE];
RC4(&rc4_key, len, (unsigned char *) buf, mybuf);
EVP_CipherUpdate(&ctx, mybuf, &outlen, (unsigned char*) buf, len);
memcpy(buf, mybuf, len);
} else {
char *end = buf + len;
......@@ -154,13 +156,24 @@ int recv_decrypt(int sock, char *buf, int len, int flags) {
return result;
}
void get_table(const char* key) {
void get_table(const char* pass) {
if (_method == RC4_ENC) {
RC4_set_key(&rc4_key, strlen(key), (unsigned char *) key);
unsigned char key[EVP_MAX_KEY_LENGTH];
unsigned char iv[EVP_MAX_IV_LENGTH];
int key_len = EVP_BytesToKey(EVP_rc4(), EVP_md5(), NULL, (unsigned char*) pass,
strlen(pass), 1, key, iv);
EVP_CIPHER_CTX_init(&ctx);
EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, 1);
if (!EVP_CIPHER_CTX_set_key_length(&ctx, key_len)) {
LOGE("Invalid key length: %d", key_len);
EVP_CIPHER_CTX_cleanup(&ctx);
exit(EXIT_FAILURE);
}
EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, 1);
} else {
uint8_t *table = encrypt_table;
uint8_t *tmp_hash = MD5((const uint8_t*)key, strlen(key), NULL);
_a = htole64(*(uint64_t *)tmp_hash);
uint8_t *tmp_hash = MD5((unsigned char *) pass, strlen(pass), NULL);
_a = htole64(*(uint64_t *) tmp_hash);
uint32_t i;
for(i = 0; i < 256; ++i) {
......
......@@ -5,7 +5,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <openssl/md5.h>
#include <openssl/rc4.h>
#include <openssl/evp.h>
#include <endian.h>
#define BUF_SIZE 4096
......@@ -15,7 +15,7 @@
unsigned char encrypt_table[256];
unsigned char decrypt_table[256];
RC4_KEY rc4_key;
EVP_CIPHER_CTX ctx;
void get_table(const char* key);
void encrypt(char *buf, int len);
......
No preview for this file type
No preview for this file type
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