This repository contains a Java implementation of the Caesar Cipher encryption and decryption technique. The Caesar Cipher is a type of substitution cipher where each letter in the plaintext is shifted a fixed number of places down or up the alphabet.
- Encryption: Encrypts a message by shifting each letter by a fixed number of positions.
- Decryption: Decrypts a message by reversing the shift.
- User Interaction: Allows users to input a message, specify a shift value, and choose between encryption and decryption.
- Input Validation: Checks for valid inputs, including shift values and non-empty strings.
The project includes two classes:
- CaeserCipher: Handles the encryption and decryption logic.
- CipherChild: Provides a user interface via the console to interact with the Caesar Cipher.
This class is responsible for the encryption and decryption logic.
-
Constructor:
public CaeserCipher(String input, int shift)
- Validates the input string and shift value.
- If the shift value is negative or larger than 25, it is normalized to fall within the range [0, 25].
-
Methods:
encryption()
: Returns the encrypted version of the input string.decryption()
: Returns the decrypted version of the input string.process(int k)
: Core method used for both encryption and decryption. Shifts each letter byk
positions.
This class provides a console-based interface for the user to interact with the Caesar Cipher.
- Workflow:
- Prompts the user to input a message.
- Prompts for the shift value.
- Allows the user to select either encryption or decryption.
- Displays the resulting message based on the chosen operation.
-
Compile the Code:
javac CaeserCipher.java CipherChild.java
-
Run the Program:
java CipherChild
!!!! Welcome to Ceaser Cipher Game !!!!
Enter your String:
Hello World
Enter the shift value:
3
Enter Your Process:
1. Encryption
2. Decryption
1
The Encrypted message is: Khoor Zruog
The Caesar Cipher works by shifting each letter of the input string by a fixed number of positions in the alphabet. For example, with a shift of 3
:
A
becomesD
B
becomesE
Z
wraps around and becomesC
For decryption, the shift is reversed.
- Non-letters: Characters like spaces, numbers, and punctuation are not changed.
- Shift Values: Any shift value (positive or negative) is normalized to the range
[0, 25]
. - Empty Input: Throws an exception if the input string is empty.
Enjoy encrypting and decrypting messages with the Caesar Cipher!