Introduction

In the realm of data security, encryption plays a crucial role in ensuring that information is transmitted securely.
Simple encryption algorithms are easy to understand and implement, providing a basic introduction to how encryption works.
The objective of this program is to demonstrate a simple encryption and decryption technique, where a given string is
encrypted by shifting the characters by a certain number of positions in the ASCII table.

Objective

The goal of this program is to create a basic encryption and decryption system using C++. In this system:

  • Encryption: The characters of the input string are shifted by a fixed number of positions.
  • Decryption: The encrypted string is restored by reversing the shift.

Program Code

#include 
#include 
using namespace std;

// Function to encrypt the string
string encrypt(string text, int shift) {
    string result = "";
    for (int i = 0; i < text.length(); i++) {
        result += char(int(text[i]) + shift);  // Shift each character by the given value
    }
    return result;
}

// Function to decrypt the string
string decrypt(string text, int shift) {
    string result = "";
    for (int i = 0; i < text.length(); i++) {
        result += char(int(text[i]) - shift);  // Reverse the shift for decryption
    }
    return result;
}

int main() {
    string inputText;
    int shiftKey;

    // Get input from user
    cout << "Enter text to encrypt: ";
    getline(cin, inputText);
    
    cout << "Enter shift value for encryption: "; cin >> shiftKey;

    // Encrypt the input text
    string encryptedText = encrypt(inputText, shiftKey);
    cout << "Encrypted text: " << encryptedText << endl;

    // Decrypt the text to verify correctness
    string decryptedText = decrypt(encryptedText, shiftKey);
    cout << "Decrypted text: " << decryptedText << endl;

    return 0;
}

Program Explanation

The program consists of two primary functions: encrypt and decrypt.

  • encrypt function: Takes a string and a shift value as input, and shifts each character of the string by the shift value.
    The shift is performed on the ASCII value of each character. The result is returned as an encrypted string.
  • decrypt function: Reverses the encryption process. It shifts the characters back by the same shift value to recover the original string.
  • main function: Prompts the user to input a string and a shift value. It then displays the encrypted text and decrypts it back to show the original message.

The program provides a basic demonstration of encryption and decryption. It uses a simple Caesar cipher-like approach,
where each character is shifted in the ASCII table.

How to Run the Program

To run the program, follow these steps:

  1. Install a C++ compiler like GCC or use an Integrated Development Environment (IDE) such as Code::Blocks or Visual Studio.
  2. Create a new C++ file (e.g., simple_encryption.cpp) and paste the provided code into it.
  3. Compile the code using the compiler or IDE. For example, using GCC:
    g++ simple_encryption.cpp -o simple_encryption
  4. Run the compiled program:
    ./simple_encryption
  5. Follow the on-screen prompts to input text and a shift value. The encrypted and decrypted results will be displayed.
© 2025 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 :)