In: Computer Science
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.
For the above problem, we need to have the following things into consideration.
Following is the JAVA program:
import java.util.ArrayList;
import java.util.List;
public class Crypt {
public static void main(String args[])
{
String message = "This is the
message";
List<Integer> crypt = new
ArrayList<Integer>();
for(int i = 0 ; i <
message.length(); i++) {
char current =
Character.toLowerCase(message.charAt(i));
if(current >=
'a' && current <= 'z') {
crypt.add(current - 'a');
}
}
System.out.print("Printing the
Crypt Message: [");
for(int i = 0
;i<crypt.size();i++) {
System.out.print(crypt.get(i) + " " );
}
System.out.println("]");
}
}
In the above program, there is a variable message in which we
need to provide the message and then crypt is the list which will
store the output.
Following is the output of the above-written program:
That was a nice
question to answer
Friend, If you have any doubts in understanding do let me know in
the comment section. I will be happy to help you further.
Please like if you think effort deserves a like.
Thanks