I have discovered that my problem is not related to RC4_INT, however, I am still having problems with keysizes and sending encrypted output between platforms... I have included code that doesn't seem to work, so if someone could point out possibly what I am doing wrong, I would greatly appreciate it!
I have included the code at the end. I compiled it using gcc on both Linux and Solaris, using the following command:
gcc -o main.c -o rc4test -I <openssldir>/include <openssldir>/libcrypto.a
When I set RC4_KEYSIZE to 152 and run the program on Linux, I get the following output:
Initial: 74 65 72 72 79 Encrypt: be 72 fe 4f 46 Decrypt: 74 65 72 72 79
When I run it on Solaris, I get the following output:
Initial: 74 65 72 72 79 Encrypt: a4 1e 73 3a de Decrypt: 74 65 72 72 79
As you can see, both programs can decrypt properly to the same initial value, but the encrypted output is different. Is there anything I'm doing wrong in the following code?
Also, I read in the O'Reilley OpenSSL book the following for RC4 (page 178):
"Discard the first 256 bytes of the generated key stream before using it. The easy way to do this is to encrypt 256 bytes of random data and discard the results."
What does this mean? I tried encrypting a block of 2048 bytes before I did the actual encryption but the output after the decryption didn't match the initial data. I tried looking for examples of this behavior on the Net but couldn't find any. However, could this be the cause of my problem?
Any help would be greatly appreciated.
Thanks,
Terry
-------------------------
#include <stdio.h>
#include "openssl/evp.h" #include "openssl/rc4.h"
#define RC4_KEYSIZE 152
int main (void) { unsigned int setKeyLen = 0; EVP_CIPHER *cipher = 0; EVP_CIPHER_CTX ctx; unsigned char iv[8]; unsigned char buffer[2048]; unsigned char *encryptOutput = buffer; int rc; unsigned char key[RC4_KEYSIZE]; int i; int encryptOutputLen; unsigned int outLen;
unsigned char *plainText = (unsigned char *) "terry"; printf("Initial:\t"); for (i=0; i < strlen(plainText); i++) printf("%02x ", plainText[i]); printf("\n");
cipher = EVP_rc4(); setKeyLen = RC4_KEYSIZE/8;
memset(&iv, 0, sizeof(iv)); memset(key, 1, sizeof(key));
/* initialize encryption */ rc = EVP_EncryptInit(&ctx, cipher, key, iv); EVP_CIPHER_CTX_set_key_length(&ctx, setKeyLen); rc = EVP_EncryptInit(&ctx, 0, key, 0);
encryptOutputLen = 0;
rc = EVP_EncryptUpdate(&ctx, encryptOutput, &encryptOutputLen, plainText, strlen(plainText));
encryptOutput += encryptOutputLen; encryptOutputLen = 0;
rc = EVP_EncryptFinal(&ctx, encryptOutput, &encryptOutputLen); encryptOutput += encryptOutputLen; encryptOutputLen = 0;
/* Get the length of the output */ encryptOutputLen = encryptOutput - buffer;
printf("Encrypt:\t"); for (i=0; i < encryptOutputLen; i++) printf("%02x ", buffer[i]); printf("\n");
/*Decrypt */ {
EVP_CIPHER_CTX ctx2; unsigned char *cipherText = buffer; unsigned int cipherTextLen = encryptOutputLen; unsigned char buffer2[2048]; unsigned char *decryptTxt = buffer2; int decryptLen = 0;
memset(buffer2, 0, sizeof(buffer2));
EVP_DecryptInit(&ctx2, cipher, key, iv); EVP_CIPHER_CTX_set_key_length(&ctx2, setKeyLen); EVP_DecryptInit(&ctx2, 0, key, 0);
rc = EVP_DecryptUpdate(&ctx2, decryptTxt, &decryptLen, cipherText, cipherTextLen);
decryptTxt += decryptLen; decryptLen = 0;
rc = EVP_DecryptFinal(&ctx2, decryptTxt, &decryptLen); decryptTxt += decryptLen; decryptLen = 0;
outLen = decryptTxt - buffer2;
printf("Decrypt:\t"); for (i=0; i < outLen; i++) printf("%02x ", buffer2[i]); printf("\n"); }
return 0; }
_________________________________________________________________
The new MSN 8: advanced junk mail protection and 2 months FREE* http://join.msn.com/?page=features/junkmail
______________________________________________________________________ OpenSSL Project http://www.openssl.org User Support Mailing List [EMAIL PROTECTED] Automated List Manager [EMAIL PROTECTED]