Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with RC4 encryption

Status
Not open for further replies.

snoopy5376

Newbie level 5
Joined
Feb 19, 2009
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,367
Hi all,
I am implementing a RC4 encryption on C++. RC4 is a stream cipher which generates a pseudorandom stream of bits (a keystream). As with any stream cipher, these can be used for encryption by combining it with the plaintext using bit-wise exclusive-or;

There are two phases:
Key Setup
1. f = ( f + Si + Kg ) mod 4
2. Swapping Si with Sf
Ciphering ( XOR)
1. i = ( i + 1 ) mod 4 , and f = ( f + Si ) mod 4
2. Swaping Si with Sf
3. t = ( Si + Sf ) mod 4
Random byte St

Below are my codes:
Code:
void prepareKey (char key[], int *s)
{
     int t[4], i, j, temp;
     cout<<"S[]= ";
     for(i=0;i<4;i++)//assigning S[]
     {
          s[i]=i;
          t[i]=key[i%strlen(key)];          
     }         
                
     for(i=0;i<4;i++) //swapping of S[]
     {
          j=(j+s[i]+t[i])%4;
          temp=s[i];
          s[i]=s[j];
          s[j]=temp;
     }
}

Code:
void rc4 (char plainText[], int s[], char *key, char *ciphertext)
{
     int m, n, i, q;
     
     for(i=0;i<16;i++)
         ciphertext[i]=0;
     cout<<"Cipher Text:";  
     for(i=0;i<strlen(plainText);i++)
     {
          int temp, temp2;
          m=(m+1)% 4;
          n=(n+s[m])% 4;
          temp=s[m];
          s[m]=s[n];
          s[n]=temp;
          q=(s[m]+s[n])%4;
          temp2 = s[q];

          ciphertext[i]=plainText[i]^temp2;
          
           cout.fill('0');
          cout.width(8);
          cout << right <<convBase(ciphertext[i],2)<<" ";                    
                    
      }
      cout<<endl;
}

I understand that the keystream generated musts be different each and every time and hence the cipher text will be different even though the same plain text is encrypted. However, what i get is always the same.

Can someone help me with this?

- - - Updated - - -

i've done some correction on it.
the S[] should not be initialize to S[]=[0,1,2,3] everytime. it should store the previous state. Can someone tell me whether this is the correct RC4 concept? thanks.

Below are my corrected codes

Code:
int main()
{         
         for(i=0;i<4;i++)//assigning S[]
         {
              S[i]=i;
              SD[i]=i;        
          }
}

Code:
void prepareKey (char key[], int *s)
{
     int g=0, i, j=0, temp=0;  
                               
     for(i=0;i<4;i++) //swapping of S[]
     {
          j=(j+s[i]+key[i%strlen(key)])%4;
          temp=s[i];
          s[i]=s[j];
          s[j]=temp;
     }
}

Code:
void rc4 (char plainText[], int s[], char *key, char *ciphertext)
{
     int m=0, n=0, i, q=0;
     
     for(i=0;i<16;i++)
         ciphertext[i]=0;
     cout<<"Cipher Text:";  
     for(i=0;i<strlen(plainText);i++)
     {
          int temp, temp2;
          m=(m+1)% 4;
          n=(n+s[m])% 4;
          temp=s[m];
          s[m]=s[n];
          s[n]=temp;
          q=(s[m]+s[n])%4;
          temp2 = s[q];

          ciphertext[i]=plainText[i]^temp2;
          
          cout.fill('0');
          cout.width(8);
          cout << right <<convBase(ciphertext[i],2)<<" ";                               
      }
      cout<<endl;
}

Below is my output:

S

[td]SD[/td]
[td]PlainText [/td]
[td]CipherText [/td]
[td]Decoded[/td]

[tr]
[td]1203[/td]
[td]1203[/td]
[td]001100001 110010[/td]
[td]001100011 110000[/td]
[td]001100001 110010[/td]
[/tr]
[tr]
[td]0321[/td]
[td]0321[/td]
[td]001100001 110010[/td]
[td]001100000 110010[/td]
[td]001100001 110010[/td]
[/tr]
[tr]
[td]0213[/td]
[td]0213[/td]
[td]001100001 110010[/td]
[td]001100010 110010[/td]
[td]001100001 110010[/td]
[/tr]
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top