Introduction
The Caesar Cipher is one of the simplest and oldest encryption techniques. It is a type of substitution cipher, where each letter in the plaintext is replaced by a letter some fixed number of positions down or up the alphabet.
This cipher is widely used in teaching the basics of cryptography due to its simplicity and ease of understanding. In this program, we will implement both the encryption and decryption processes of the Caesar Cipher in Java. The main objective of this program is to demonstrate how to shift characters in a string using a specified key and reverse the process to decrypt the message.
Objective
The primary objective of this program is to:
- Implement the Caesar Cipher encryption algorithm
- Implement the Caesar Cipher decryption algorithm
- Allow the user to input a message and a shift key
- Provide clear instructions for using the program and understanding the output
Code: Caesar Cipher in Java
import java.util.Scanner;
public class CaesarCipher {
// Method to encrypt the plain text
public static String encrypt(String plainText, int shift) {
StringBuilder cipherText = new StringBuilder();
// Loop through each character of the plain text
for (int i = 0; i < plainText.length(); i++) {
char c = plainText.charAt(i);
// Encrypt only alphabetic characters
if (Character.isLetter(c)) {
char base = Character.isLowerCase(c) ? 'a' : 'A';
// Shift character within alphabet
c = (char) ((c - base + shift) % 26 + base);
}
cipherText.append(c);
}
return cipherText.toString();
}
// Method to decrypt the cipher text
public static String decrypt(String cipherText, int shift) {
return encrypt(cipherText, 26 - shift); // Decryption is simply encryption with reverse shift
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Take input for message and shift value
System.out.println("Enter the message to encrypt: ");
String message = scanner.nextLine();
System.out.println("Enter the shift value (key): ");
int shift = scanner.nextInt();
// Encrypt the message
String encryptedMessage = encrypt(message, shift);
System.out.println("Encrypted Message: " + encryptedMessage);
// Decrypt the message
String decryptedMessage = decrypt(encryptedMessage, shift);
System.out.println("Decrypted Message: " + decryptedMessage);
scanner.close();
}
}
Explanation of the Program
The program begins by importing the necessary classes. In this case, we are using the Scanner
class to take input from the user. The program has two main methods: encrypt
and decrypt
.
1. Encrypt Method:
– This method takes two parameters: the message (plain text) to be encrypted and the shift key.
– The method iterates through each character in the input string and checks if it is a letter. Non-alphabetic characters (such as spaces, numbers, etc.) are not modified.
– If the character is a letter, it computes the new character by shifting it by the specified key while keeping it within the bounds of the alphabet (i.e., using modulus arithmetic to wrap around the alphabet).
– Finally, the modified character is added to a StringBuilder, which holds the encrypted message.
2. Decrypt Method:
– Decryption in the Caesar Cipher is simply the reverse of encryption. The decrypt
method calls the encrypt
method but with the key shifted by the reverse of the encryption key (i.e., 26 – shift).
– This ensures that the original message is retrieved.
3. Main Method:
– The main
method asks the user for the message they want to encrypt and the key (shift value).
– It then calls the encrypt
method to encrypt the message and displays the encrypted result.
– Following this, it calls the decrypt
method to retrieve the original message, and prints the decrypted message.
How to Run the Program
To run the program, follow these steps:
- Open a Java IDE (like Eclipse or IntelliJ IDEA) or use a simple text editor (like Notepad++) to write the program.
- Copy and paste the provided Java code into a new file named
CaesarCipher.java
. - Compile the program using the following command in the terminal or command prompt:
javac CaesarCipher.java
- Run the compiled program using the command:
java CaesarCipher
- The program will prompt you for the message and the shift key. After you input these, the program will display the encrypted and decrypted messages.
Example Usage:
If you enter the message “HELLO” with a shift value of 3, the encrypted message will be “KHOOR”, and the decrypted message will be “HELLO”.