Hi Nilesh,

Here's the rc4 example I have come up with:

#include <stdio.h>
#include <string.h>
#include <openssl/rc4.h>
#include <openssl/bio.h>


int main(void)
{

    int     i;
    BIO*    bio_out;
    RC4_KEY key;

    static unsigned char key_data[256] = {
        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
    };

    char*   data_to_encrypt  = "Luck is where preparation meets
opportunity.";

    int     length = (int) strlen(data_to_encrypt);

    /* Allocate some memory for the ciphertext */
    unsigned char*  ciphertext = (unsigned char*) malloc(sizeof(char) *
length);

    /* Allocate some memory for the decrypted ciphertext (plaintext) */
    unsigned char*  plaintext  = (unsigned char*) malloc(sizeof(char) *
length);

    RC4_set_key(&key, 8, key_data);
    RC4(&key, length, data_to_encrypt, ciphertext);

    bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);

    BIO_printf(bio_out, "Original plaintext: %s\n\n", data_to_encrypt);

    BIO_printf(bio_out, "Ciphertext: ");

    /* print out the ciphertext */
    for (i = 0; i < length; i++)
        BIO_printf(bio_out, "%02x", ((unsigned char*)ciphertext)[i]);

    BIO_printf(bio_out, "\n\n");

    /* The decryption process */

    /* First, we reset the key stream */
    RC4_set_key(&key, 8, key_data);

    /* Carry out the decryption */
    RC4(&key, length, ciphertext, plaintext);

    /* Print out the recovered plaintext */
    BIO_printf(bio_out, "Recovered plaintext: ");

    for (i = 0; i < length; i++)
        BIO_printf(bio_out, "%c", ((unsigned char*)plaintext)[i]);

    BIO_printf(bio_out, "\n\n");

    BIO_free(bio_out);

    free(ciphertext);
    free(plaintext);

    return 0;
}

Output:

Original plaintext: Luck is where preparation meets opportunity.

Ciphertext:
38e1a18c30227b597a23b02157af6e8efe6a893aeff455183c1525d213f8b8f9a9fbfc45dad91267357b2bfa

Recovered plaintext: Luck is where preparation meets opportunity.

As you can probably see in the example above, RC4_set_key sets up the
RC4_KEY structure, key, using 8 bytes of data declared in key_data.

Hope that helps :-)


Regards,
Anantha


On Tue, Nov 1, 2011 at 10:53 PM, nilesh <nilesh.tay...@netscout.com> wrote:

> On Tuesday 01 November 2011 04:18 PM, nilesh wrote:
>
>> Hi,
>>
>> I am a newbie in the cryptography area and learning by writing some test
>> code.
>> I have setup the apache server and capturing packets using wireshark.
>> I have a query specific to RC4. With the given server private key, I am
>> able to generate master secret and key block correctly.
>> My test code output matches with Wireshark debug logs (i.e. the master
>> secret generated and key block generated using pseudo-random function).
>>
>> However, I am unable to understand how to use the RC4_set_key() and
>> RC4() APIs.
>> What exact part of key_block should I feed to RC4_set_key()?
>> key_block[0..15] - client MAC
>> key_block[16..31] - server MAC
>> key_block[32..48] - client Write key
>> key_block[49..64] - server Write key
>>
>> Please see the test code attached (please pardon the code formatting).
>>
>>
> Does someone have any hint or test code? Or please redirect to appropriate
> list, if this is not the place to post this query.
>
> --
> Thanks,
> Nilesh
> ______________________________**______________________________**__________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    openssl-users@openssl.org
> Automated List Manager                           majord...@openssl.org
>

Reply via email to