Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a program in c++ that Implement the simple Caesar cipher (letter + key) mo

ID: 3601703 • Letter: W

Question

write a program in c++ that Implement the simple Caesar cipher (letter + key) mod 26 decipher (letter - key) mod 26. Remember to create FUNCTIONS so that you main() program is small; for example: Caesa Encryption(char *in, char *out, int length, int key) { ..... } your main should ask user for a sentence and a key, then encrypt it, showing the user the cipher. The user should be able to decrypt as well. Can you please solve this problem in the simplest way possible without using the function strcpy(letters, alphabets.c_str());

Explanation / Answer

#include <iostream>
#include <string>

using namespace std;

string encrypt(string text, int key) {
string result = "";
for (int i = 0; i < text.length(); i++) {
result += 'A' + (((text[i] - 'A') + key)%26);
}
return result;
}

string decrypt(string cipher, int key) {
string result = "";
for (int i = 0; i < cipher.length(); i++) {
int diff = (((cipher[i] - 'A') - key)%26);
if (diff < 0) {
diff = (diff + 26)%26;
}
result += 'A' + diff;
}
return result;
}

int main()
{
cout << "Enter a text to encrypt: ";
string text;
cin >> text;

cout << "Enter key: ";
int key;
cin >> key;

cout << "Cipher text: " << encrypt(text, key);

cout << "Enter a ciher to decrypt: ";
cin >> text;

cout << "Enter key: ";
cin >> key;

cout << "Text: " << decrypt(text, key);

return 0;
}