Heckroth Industries

RC4 Tutorial

The RC4 algorithm is a stream cipher. It was created by Ron Rivest of “RSA Data Security” in 1987. The algorithm produces a key stream that is Xor’d with the plain text. At the core of the algorithm is an 16×16 SBox (Substitution Box) which is initialised by the key. Every time a byte is produced the SBox is permuted.

The algorithm needs to share 3 variables between the two functions (i, j and SBox[]);

To initialise the SBox the following procedure is followed

  1. Each element in the SBox is filled with its element number. (I.E SBox[0]=0, SBox[1]=1, …..SBox[255]=255)
  2. Another array of equal size to the SBox (K) is filled with the key. Repeating the key as needed until the whole of the array has been filled.
  3. The index j is set to 0 4 : for(i=0 to 255) j=(j+SBox[i]+K[j]) Mod 256 Swap SBox[i] with SBox[j] done

At the end of this process the key array (K) is no longer needed. To the next value in the key stream the following procedure is used

  1. i = (i + 1) Mod 256
  2. j = (j + SBox[i]) Mod 256
  3. Swap SBox[i] with SBox[j]
  4. t = (SBox[i] + SBox[j]) Mod 256
  5. return SBox[t]

Because RC4 uses Xor to encipher the plain text then the decipher process is exactly the same as the encipher process.

The following three files provide the basis for an rc4 encryption. To use it just compile rc4.c as an object file and link it to you program file (remembering to add rc4.h to the list of include files in your program).

e.g. gcc -C -o rc4.o rc4.c gcc -o rc4 test.c rc4.o

The generateKey function will produce the SBox based upon the key passed to it. The getByte function will return the next value in the key stream.

Note that this only provides a key stream and your program will need to Xor this with the plain text it is wishing to encipher.

The test.c program provided gives an example of how to use rc4 in your own programs.

----------------- Begin rc4.h ----------------
/*
* rc4 ecryption scheme header
*
*/

#ifndef RC4HEADER
#define RC4HEADER

void generateKey(unsigned char Key[]);
unsigned char getByte(void);

#endif
----------------- End rc4.h ----------------
----------------- Begin rc4.c ----------------
/*
* rc4 ecryption scheme
*
*/

#include <stdio.h>
#include <string.h>

#define SIZE 256

unsigned char SBox[SIZE];
int i;
int j;

void generateKey(unsigned char Key[]);
unsigned char getByte(void);

void generateKey(unsigned char Key[]) {
    unsigned char tmp;
    unsigned char KBox[SIZE];

    for(i = 0; i < SIZE; i++)
        SBox[i] = i;

    for (i = 0; i < SIZE; i++)
        KBox[i] = Key[i % strnlen(Key, SIZE)];

    for (j = 0, i = 0; i < SIZE; i++) {
        j = (j + SBox[i] + KBox[i]) % SIZE;
        tmp = SBox[i];
        SBox[i] = SBox[j];
        SBox[j] = tmp;
    }
}

unsigned char getByte(void) {
    unsigned char tmp;

    i = (i + 1) % SIZE;
    j = (j + SBox[i]) % SIZE;
    tmp = SBox[i];
    SBox[i] = SBox[j];
    SBox[j] = tmp;

    return SBox[(SBox[i] + SBox[j]) % SIZE];
}
----------------- End rc4.c ----------------
----------------- Begin test.c ----------------
/*
* rc4 ecryption scheme
*
*/

#include "rc4.h"
#include <stdio.h>

int main(int argc, char **argv) {
    int in;
    if (argc > 1) {
        generateKey(argv[1]);
        while((in = getchar()) != EOF) {
            printf("%c", (unsigned char) in ^ getByte());
        }
    }
    return 0;
}
----------------- End test.c ------------------
Jason — 2009-12-04