0% found this document useful (0 votes)
3 views

6caesar Cipher

Uploaded by

Karishama Devani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

6caesar Cipher

Uploaded by

Karishama Devani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Caesar 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

You might also like