# RC4 Encryption

Page 1 of 1

## 2 Replies - 36843 Views - Last Post: 11 January 2010 - 10:44 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=149436&amp;s=061653ef0ae3661a72c9ed0cbd650300&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 awfa3

Reputation: 0
• Posts: 55
• Joined: 03-July 09

# RC4 Encryption

Posted 11 January 2010 - 08:44 PM

So I'm looking for an rc4 encryption for C but so-far, I've found only the one in wikipedia. Can someone post an example code of it or simplify the one at wikipedia for me?

Heres the one from wikipedia...
```unsigned char S[256];
unsigned int i, j;

void swap(unsigned char *s, unsigned int i, unsigned int j) {
unsigned char temp = s[i];
s[i] = s[j];
s[j] = temp;
}

/* KSA */
void rc4_init(unsigned char *key, unsigned int key_length) {
for (i = 0; i < 256; i++)
S[i] = i;

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

i = j = 0;
}

/* PRGA */
unsigned char rc4_output() {
i = (i + 1) & 255;
j = (j + S[i]) & 255;

swap(S, i, j);

return S[(S[i] + S[j]) & 255];
}

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

#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))

int main() {
unsigned char *test_vectors[][2] =
{
{"Key", "Plaintext"},
{"Wiki", "pedia"},
{"Secret", "Attack at dawn"}
};

int x;
for (x = 0; x < ARRAY_SIZE(test_vectors); x++) {
int y;
rc4_init(test_vectors[x][0], strlen((char*)test_vectors[x][0]));

for (y = 0; y < strlen((char*)test_vectors[x][1]); y++)
printf("%02X", test_vectors[x][1][y] ^ rc4_output());
printf("\n");
}
return 0;
}

```

Edit:

Code I have so far which is failing
```#include <stdio.h>

void swap(char *a, char *B);

int main() {
int i;
int j = 0;
int temp;
char s[256];
char key[] = "Test";
char data[] = "Hello";
for (i = 0; i < 256; ++i)
s[i] = i;
for (i = 0; i < 256; ++i) {
j = (j + s[i] + key[i%4]) % 256;
swap(&s[i], &s[j]);
}

i = (i+1) % 256;
j = (j + s[i]) % 256;
swap(&s[i], &s[j]);
// I don't know where to go from here....
}

void swap(char *a, char *B) {
int temp;
temp = *a
*a = *b;
*b = temp;
}
```

This post has been edited by awfa3: 11 January 2010 - 08:58 PM

Is This A Good Question/Topic? 0

## Replies To: RC4 Encryption

### #2 awfa3

Reputation: 0
• Posts: 55
• Joined: 03-July 09

## Re: RC4 Encryption

Posted 11 January 2010 - 09:33 PM

Ok, I wrote another code that kind of works but it doesn't really because it isn't ciphering it correctly.
```#include <stdio.h>

#define swap(i, j) char t = i; i = j; j = t;

int main() {
int i;
int iter;
int j = 0;
int temp;
char s[256];
char key[] = "Test";
char data[] = "Hello";
for (i = 0; i < 256; ++i)
s[i] = i;
for (i = 0; i < 256; ++i) {
j = (j + s[i] + key[i%4]) % 256;
swap(s[i], s[j]);
}
printf("Hex Dump of Cipher Text: ");
for (iter = 0; data[iter] != '\0'; ++iter) {
i = (i+1) % 256;
j = (j + s[i]) % 256;
swap(s[i], s[j]);
printf("%x ", (unsigned char)(s[(s[i] + s[j]) %256] ^ data[iter]));
}
printf("\nOriginal Hex Dump of Text: ");
printf("%x %x %x %x %x", data[0], data[1], data[2], data[3], data[4]);
getchar();
}

```

The result of this is FC 16 2C E7 5C
But the correct RC4 output would be 32 0B 21 24 2D
As you can see its different. Can someone help me on this?

### #3 bodom658

• Villiage Idiom

Reputation: 114
• Posts: 1,123
• Joined: 22-February 08

## Re: RC4 Encryption

Posted 11 January 2010 - 10:44 PM

I would start by hooking it up to a debugger, and seeing if you can find errors that way.

Also, writing code line by line and writing out what each line does (basically manual debugging) can be really helpful.