Implementing RC4 Algorithm
Code :import.java.io.*;
class RC4Demo
{
String strPlain;
static char cipher[];
RC4Demo(String strPlain,int[] key)
{
this.strPlain=strPlain;
int s[]=new int[255];
cipher=new char[strPlain.length()];
for(int i=0;i<s.length;i++)
{
s[i]=i;
}
int i=0;
int j=0;
for(int k=0;k<strPlain.length();k++)
{
int modk=(k%key.length);
int kc=key[modk];
j=(s[i]+j+kc)%256+1;
int temp=s[i];
s[i]=s[j];
s[j]=temp;
int sc=(s[i]+s[j])%256;
int ck=s[sc];
cipher[k]=(char)(ck^(int)strPlain.charAt(k));
i=i+1;
}
}
public static void main(String[] args)
{
int k[]={1,2,3,4,5};
String strOriginal="hello world";
System.out.println("Original String---->"+strOriginal);
new RC4Demo(strOriginal,k);
for(int i=0;i<cipher.length;i++)
{
System.out.print(" "+cipher[i]);
}
}
}
If the answers is incorrect or not given, you can answer the above question in the comment box. If the answers is incorrect or not given, you can answer the above question in the comment box.