Introduction
The Caesar Cipher is one of the simplest and most well-known encryption techniques. It is a type of substitution cipher where each letter in the plaintext is shifted by a fixed number of positions down the alphabet. In this program, we will implement both the encryption and decryption functions of the Caesar Cipher using C++.
In encryption, we take the plaintext message and shift each letter by a fixed number (key). For decryption, we reverse this operation using the same key to recover the original message.
Objective
The objective of this program is to demonstrate the implementation of the Caesar Cipher in C++. The program will perform two primary functions:
- Encryption: Convert plaintext to ciphertext using a specified shift.
- Decryption: Convert the ciphertext back to the original plaintext using the same shift.
The program will also allow the user to enter a custom shift value (key) for encryption and decryption operations.
Program Code
#include #include using namespace std; // Function to encrypt plaintext using Caesar Cipher string encrypt(string plaintext, int shift) { string ciphertext = ""; for (char c : plaintext) { if (isalpha(c)) { char base = islower(c) ? 'a' : 'A'; ciphertext += (c - base + shift) % 26 + base; } else { ciphertext += c; // Non-alphabet characters remain unchanged } } return ciphertext; } // Function to decrypt ciphertext using Caesar Cipher string decrypt(string ciphertext, int shift) { string plaintext = ""; for (char c : ciphertext) { if (isalpha(c)) { char base = islower(c) ? 'a' : 'A'; plaintext += (c - base - shift + 26) % 26 + base; } else { plaintext += c; // Non-alphabet characters remain unchanged } } return plaintext; } int main() { string text; int shift; int choice; cout << "Caesar Cipher Program\n"; cout << "1. Encrypt\n"; cout << "2. Decrypt\n"; cout << "Enter your choice (1 or 2): "; cin >> choice; cin.ignore(); // To ignore the leftover newline character from the previous input cout << "Enter the text: "; getline(cin, text); cout << "Enter the shift value (key): "; cin >> shift; if (choice == 1) { string encryptedText = encrypt(text, shift); cout << "Encrypted text: " << encryptedText << endl; } else if (choice == 2) { string decryptedText = decrypt(text, shift); cout << "Decrypted text: " << decryptedText << endl; } else { cout << "Invalid choice! Please choose either 1 or 2.\n"; } return 0; }
Explanation of the Program Structure
The program consists of the following components:
- encrypt function: This function takes the plaintext and a shift value as input. It loops through each character in the input string and shifts it by the specified key. It handles both lowercase and uppercase letters while leaving non-alphabetic characters unchanged.
- decrypt function: This function reverses the encryption process. It takes the ciphertext and shift value as input, and shifts each letter in the opposite direction to recover the original plaintext.
- main function: The main function allows the user to choose between encryption and decryption, input the text, and specify the shift value. Based on the user’s choice, the program calls either the
encrypt
ordecrypt
function and displays the result.
We use the isalpha
function to check if the character is an alphabet letter. Non-alphabet characters, such as spaces or punctuation, are not altered during the encryption or decryption process.
How to Run the Program
To run this C++ program on your machine, follow these steps:
-
- Ensure that you have a C++ compiler installed, such as GCC (GNU Compiler Collection) or MinGW.
- Create a new text file with a
.cpp
extension (e.g.,caesar_cipher.cpp
) and copy the code provided above into the file. - Open a terminal or command prompt, navigate to the directory where the file is saved, and compile the program using the following command:
g++ -o caesar_cipher caesar_cipher.cpp
-
- Once the program is compiled, run it with the following command:
./caesar_cipher
- Follow the on-screen prompts to enter the text, choose encryption or decryption, and specify the shift value.
The program will then output either the encrypted or decrypted text based on your input.