June 26th, 2012, 05:49 AM

RC4 and RC4A
Hi All,
I've implemented RC4 on C++. However, it is said that RC4 is not secure enough and RC4A is designed to enhance the security of encryption. I'm trying to implement RC4A on C++ but I find it quite hard to look for a RC4A code in the internet. I've tried to write it out, but the result of it is the same for every iteration. Can someone help me to check where have I gone wrong? Thank you very much for the help.
Below is my code for RC4. I refer to the code on this site (http://read.pudn.com/downloads126/sourcecode/crypt/532804/RC4.CPP__.htm). The cipher text for every iteration is different (for same plain text), which I consider correct.
Code:
void prepareKey (int *key, int keyLength, int *s )
{
int i=0, j=0, temp, t[8];
for(int i=0;i<8;i++)
{
s[i]=i;
t[i]=key[i%keyLength];
}
for(i=0;i<8;i++) //swapping of S[]
{
j=(j+s[i]+t[i])%8;
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
void rc4 (int plainText[], int pTextLength, int *s, int *key, int *ciphertext)
{
int m=0, n=0, i=0, q=0;
int temp;
for(i=0;i<pTextLength;i++)
{
m=(m+1)%8;
n=(n+s[m])%8;
temp=s[m];
s[m]=s[n];
s[n]=temp;
q=(s[m]+s[n])%8;
key[i]=s[q];
ciphertext[i]=plainText[i]^key[i];
}
}
Here is my problem, the RC4A implementation. I refer to the algorithm of RC4A in Wikipedia (http://en.wikipedia.org/wiki/RC4). Below is my RC4A code. The cipher text for every iteration (for same plain text) is the same. Where have I gone wrong?
p/s, i notice that unlike RC4, the key is not being altered. Where should i alter it?
Code:
void prepareKey (int *key, int keyLength, int *s1, int *s2 )
{
int i=0, j=0, temp, t[8];
for(int i=0;i<8;i++)
{
s1[i]=i;
t[i]=key[i%keyLength];
s2[i]=i;
}
for(i=0;i<8;i++) //swapping of S[]
{
j=(j+s[i]+t[i])%8;
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
void rc4A (int plainText[], int pTextLength, int *s1, int *s2, int *key, int *ciphertext)
{
int i=0, j1=0, j2=0;
int temp, temp2;
int p, q;
for(int k=0;k<pTextLength;k++)
{
i=(i+1)%8;
j1=(j1+s1[i])%8;
temp=s1[i];
s1[i]=s1[j1];
s1[j1]=temp;
p=(s1[i]+s1[j1])%8;
j2=(j2+s2[i])%8;
temp2=s2[i];
s2[i]=s2[j2];
s2[j2]=temp2;
q=(s2[i]+s2[j2])%8;
ciphertext[k]=plainText[k]^s2[p];
ciphertext[k]=ciphertext[k]^s1[q];
}
}
Thanks for the help again.