cplusplus
cplusplus

 

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:

  1. 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.
  2. 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.
  3. 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 or decrypt 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:

    1. Ensure that you have a C++ compiler installed, such as GCC (GNU Compiler Collection) or MinGW.
    2. Create a new text file with a .cpp extension (e.g., caesar_cipher.cpp) and copy the code provided above into the file.
    3. 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
    1. Once the program is compiled, run it with the following command:
./caesar_cipher
  1. 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.

Copyright © 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)