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