6caesar Cipher
6caesar Cipher
#include<iostream>
#include<cctype>
using namespace std;
string CaesarEncrypt(string plaintext, int key) {
string ciphertext = "";
for (char& ch : plaintext) {
if (isalpha(ch)) {
char base = isupper(ch) ? 'A' : 'a';
ch = (ch - base + key) % 26 + base;}
ciphertext += ch; }
return ciphertext;}
string CaesarDecrypt(string ciphertext, int key) {
return CaesarEncrypt(ciphertext, 26 - key); }
int main() {
string plaintext;
int key;
cout << "Enter plaintext: ";
getline(cin, plaintext);
cout << "Enter key (an integer): "; cin >> key;
string ciphertext = CaesarEncrypt(plaintext, key);
cout << "Ciphertext: " << ciphertext << "\n";
string decryptedText = CaesarDecrypt(ciphertext, key);
cout << "Decrypted text: " << decryptedText << "\n";}
Output