#include <iostream>
#include <string>
using namespace std;
// Function to encrypt a message using Caesar cipher
string encrypt(string plaintext, int shift) {
string ciphertext = "";
for (char c : plaintext) {
if (isalpha(c)) { // Check if the character is a letter
char base = islower(c) ? 'a' : 'A';
char encryptedChar = (c - base + shift) % 26 + base;
ciphertext += encryptedChar;
} else {
ciphertext += c; // Non-alphabetic characters remain unchanged
}
}
return ciphertext;
}
// Function to decrypt a message using Caesar cipher
string decrypt(string ciphertext, int shift) {
return encrypt(ciphertext, 26 - shift); // Decryption is just encryption with the inverse shift
}
int main() {
string text;
int shift;
// Input plaintext and shift value
cout << "Enter the text: ";
getline(cin, text);
cout << "Enter shift value (1-25): ";
cin >> shift;
// Validate shift value
if (shift < 1 || shift > 25) {
cerr << "Error: Shift value must be between 1 and 25." << endl;
return 1;
}
// Encrypt the text
string encryptedText = encrypt(text, shift);
cout << "Encrypted text: " << encryptedText << endl;
// Decrypt the text
string decryptedText = decrypt(encryptedText, shift);
cout << "Decrypted text: " << decryptedText << endl;
return 0;
}