In: Computer Science
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using the word CODES as the key:
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. Ignore spaces and punctuations and put the message in groups of five letters.
First, Let's see how code is encrypted..
Java Code of implementation is given below:
CryptoCode.java file
public class CryptoCode
{
public static StringBuffer
code_encryption(String texttoencrypt, int shiftnumber)
{
StringBuffer result= new
StringBuffer();
for (int i=0;
i<texttoencrypt.length(); i++)
{
if (Character.isUpperCase(texttoencrypt.charAt(i)))
{
char character = (char)(((int)texttoencrypt.charAt(i) + shiftnumber
- 65) % 26 + 65);
result.append(character);
}
else
{
char character = (char)(((int)texttoencrypt.charAt(i) +
shiftnumber - 97) % 26 + 97);
result.append(character);
}
}
return result;
}
public static void main(String[] args)
{
String texttoencrypt =
"WHATISNEXTMOVE";
int shiftnumber =
3;
System.out.println("Command : " + texttoencrypt);
System.out.println("Shift number : " + shiftnumber);
System.out.println(code_encryption(texttoencrypt,
shiftnumber));
}
}
Code encryption and decryption ultimately can generate using given formula
Encrypt(n) = DeCrypt(26-n)
Where n is shift-number