1

i would like to know how can i use a set of numbers as a KEY for the rc4 encryption. According to the internet and wiki the KEY is actually a string of letters but the bytes are used . But in my program i need to use a 6 digit number as a KEY. Should i covert it to a string or how.

Key Sheudling Algorithm is indicated below.

void ksa(u_char *State, u_char *key) {
int byte, i, keylen, j=0;

keylen = (int) strlen((char *) key);

for(i=0; i<256; i++) {
    j = (j + State[i] + key[i%keylen]) % 256;
    swap(&State[i], &State[j]);
}

How can i modify the code or should i just convert the numbers to string.

M.A
  • 1,073
  • 2
  • 15
  • 21

1 Answers1

1

Strings and numbers are both bytes. Here is a working RC4 code that accepts a key of unsigned chars:

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

#define SIZE 256

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

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

void initRC4(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];
}

First, you initialize the RC4 stream:

initRC4(key);

Then you do:

getByte()

...which always returns 1 byte from the RC4 stream you've set up.

One thing to remember though - a letter in string is not always equal to 1 byte. Same goes for the integers and number symbols in strings. Really, you must read an introduction to computer programming before you mess with ciphers.

Here is a demonstration of how bytes differ in strings in integers:

#include <string>

int main(int argc, char **argv) {

    const int n=67898;
    const std::string str = "67898";    

    const int arrayLength = sizeof(int);
    const int stringArrayLength = str.size();
    unsigned char *bytePtr=(unsigned char*)&n;


    printf("Bytes for integer: ");
    for(int i=0;i<arrayLength;i++)
    {
       printf("%X ", bytePtr[i]);
    }
    printf("\n");

    printf("Bytes for string: ");
    for(int i=0;i<stringArrayLength;i++)
    {
       printf("%X ", str.at(i));
    }
    printf("\n");


    return 0;

}

Output:

Bytes for integer: 3A 9 1 0
Bytes for string: 36 37 38 39 38

There will usually be a terminating byte at the end of a string, so you could add +1 byte to string size.

Kai
  • 424
  • 5
  • 16
  • Thanks for the code. But i would like to know how to pass the key as set of integers. Your initRC4(key) accepts char. Can i pass in the numbers just like that. initRC4(67898); – M.A May 01 '13 at 13:59
  • 67898 would be an integer here. That would initialize your RC4 with bytes 1093A (67898 in decimal = 1093A in hexadecimal). I have updated my answer with code. – Kai May 01 '13 at 14:59
  • Thanks alot now i understand how it works. Do correct me if i am wrong. SO i use your getByte() method to get the bytes of my integer which i will pass it to me initRC4()method. Am i right? – M.A May 01 '13 at 17:23
  • Nop. First, you initialize RC4 stream with your key (bytes from a string or an integer). Then, you pull bytes of the RC4 stream with that getByte() function and put them to use. If you are sure that your integer will always be a valid integer (no 000034, 012345 and alike prior to type conversion), just get the bytes from it and init the RC4 stream with them. – Kai May 01 '13 at 18:02