RC4 Encryption Algorithm > Java Program

System Security

RC4 Encryption Algorithm > Java Program


In cryptography  is most widely used software stream cipher and is used popular protocols such as secure socket layer(SSL) and wep. RC4 has variable length key. RC4 is developed by Ronald Rivest which require secure exchange of shared key..RC4 algorithm operates in OFB (output feedback mode) ,where block cipher is implemented as stream cipher.RC4 generates pseudo-random stream of bits.In RC4 algorithm key stream is completely independent of plain text. 

import java.io.*; 
class rc4 


public static void main(String args[])throws IOException 

int temp=0; 
String ptext; 
String key; 
int s[]=new int[256]; 
int k[]=new int[256]; 
DataInputStream in=new DataInputStream(System.in); 
System.out.print("\nENTER PLAIN TEXT\t"); 
ptext=in.readLine(); 
System.out.print("\n\nENTER KEY TEXT\t\t"); 
key=in.readLine(); 
char ptextc[]=ptext.toCharArray(); 
char keyc[]=key.toCharArray(); 
int cipher[]=new int[ptext.length()]; 
int decrypt[]=new int[ptext.length()]; 
int ptexti[]=new int[ptext.length()]; 
int keyi[]=new int[key.length()]; 
for(int i=0;i<ptext.length();i++) 

ptexti[i]=(int)ptextc[i]; 

for(int i=0;i<key.length();i++) 

keyi[i]=(int)keyc[i]; 

for(int i=0;i<255;i++) 

s[i]=i; k[i]=keyi[i%key.length()]; 

int j=0; 
for(int i=0;i<255;i++) 

j=(j+s[i]+k[i])%256; 
temp=s[i]; 
s[i]=s[j]; 
s[j]=temp; 

int i=0; 
j=0; 
int z=0; 
for(int l=0;l<ptext.length();l++) 

i=(l+1)%256; 
j=(j+s[i])%256; 
temp=s[i]; 
s[i]=s[j]; 
s[j]=temp; 
z=s[(s[i]+s[j])%256]; 
cipher[l]=z^ptexti[l]; 
decrypt[l]=z^cipher[l]; 

System.out.print("\n\nENCRYPTED:\t\t"); 
display(cipher); 
System.out.print("\n\nDECRYPTED:\t\t"); 
display(decrypt); 

static void display(int disp[]) 

char convert[]=new char[disp.length]; 
for(int l=0;l<disp.length;l++) 

convert[l]=(char)disp[l]; 
System.out.print(convert[l]); 



/* RC4 Encryption Algorithm Program Output : 
ENTER PLAIN TEXT        RC4 PROGRAM 
ENTER KEY TEXT          A 
ENCRYPTED:              ??-??±?µFJ| 
DECRYPTED:              RC4 PROGRAM 
*/

Comments

Popular posts from this blog

Intermediate Code Generation > C Program