Write a Java program to encrypt the following message using the RC4 cipher using key CODES:
Cryptography is a method of protecting information and communications through the use of codes so that only those for whom the information is intended can read and process it.
Instead of using stream length 256, we will use length 26. When encrypting, let A = 0 to Z = 25 (hence CODES = [2 14 3 4 18]). Ignore spaces and punctuations and put the message in groups of five letters.
import java.util.*;
import java.lang.*;
class Main
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
String s = "";
s = s + sc.nextLine ();
String inp = "";
int i = 0;
for (i = 0; i < s.length (); i++)
{
if (s.charAt (i) >= 'A' && s.charAt (i) <= 'Z')
{
inp += s.charAt (i);
}
}
int l=inp.length();
int siz=l/5;
if(l%5!=0)
siz++;
String[] out=new String[siz];
for(i=0;i<siz;i++)
out[i]="";
for(i=0;i<l;i++)
{
int tmp=inp.charAt(i)-'A';
String teemp=String.valueOf(tmp);
teemp=teemp+" ";
out[i/5]+=teemp;
}
for(i=0;i<siz;i++)
System.out.println(out[i]);
}
}
Get Answers For Free
Most questions answered within 1 hours.