Caesar cipher is a type of substitution ciphers in which letters in a message (called plaintext)
are replaced with other letters to generate a ciphertext. The choice of replacement characters
is a function of a key (or a password). The key should be a number between 1 and 26. In
encryption (converting plaintext to ciphertext), the letter is replaced by another that is key
letters after it, whereas, in decryption (converting ciphertext to plaintext) the letter is replaced
by another that is key letters before. The letter after 'Z' is considered to be 'A'. For example, if
the key is 2: Encrypting plaintext Apple ciphertext CRRNG. Decrypting ciphertext CRRNG
plaintext: APPLE. Your program should do the following Ask the user whether he/she wants
to do encryption or decryption. Validate that the user inputs either 'e' for encryption or 'd' for
decryption. Ask the user for a message that only has alphabet letters with no spaces. Check
the message and remove any non-alphabet letter while displaying a warning, and make all
the alphabet letters uppercase. Ask the user for a key between 1 and 26 (inclusive). Validate
that the key is within this range. Compute and display the ciphertext if the user chose
encryption or the plaintext if the user chose decryption. Your program should display
messages identical to the examples below given the same input. Hello! Please press e for
encryption and d for decryption e Please enter your Message. The Message should have only
Alphabet letters, all spaces and non-alphabet characters will be deleted. I love you! Non
Alphabet Character Dropped: Non Alphabet Character Dropped: Non Alphabet Character
Dropped! Please input a key between 1 and 26: 7 The ciphertext is:PSVCLFVB
Solution
import javax.swing.JOptionPane;
public class CaesarCipher
public static void main(String[] args)
{
String str = (JOptionPane.showInputDialog("Input Data to encypt:"));
String key = (JOptionPane.showInputDialog("Input the key:"));
int keyLength=key.length();
//prints encryption
String encrypted = encrypt(str, keyLength);
System.out.println("Encrypted:" + encrypted);
//prints decryption
String decrypted = decrypt(encrypted, keyLength);
System.out.println("Decrypted:" + decrypted);
//prints key
System.out.println("Key:" + key);
}
public static String encrypt(String str, int keyLength)
{
String encrypted = "";
for(int i = 0; i < str.length(); i++)
{
int c = str.charAt(i);
if (Character.isUpperCase(c))
{
//26 letters of the alphabet so mod by 26
c = c + (keyLength % 26);
if (c > 'Z')
c = c - 26;
}
else if (Character.isLowerCase(c))
{
c = c + (keyLength % 26);
if (c > 'z')
c = c - 26;
}
encrypted += (char) c;
}
return encrypted;
}
public static String decrypt(String str, int keyLength)
{
String decrypted = "";
for(int i = 0; i < str.length(); i++)
{
int c = str.charAt(i);
if (Character.isUpperCase(c))
{
c = c - (keyLength % 26);
if (c < 'A')
c = c + 26;
}
else if (Character.isLowerCase(c))
{
c = c - (keyLength % 26);
if (c < 'a')
c = c + 26;
}
decrypted += (char) c;
}
return decrypted;
}