Untitled
unknown
plain_text
10 months ago
898 B
3
Indexable
#include<iostream>
using namespace std;
string encrypt(string msg, int key);
string decrypt(string msg, int key);
int main() {
string msg;
int key;
cout << "Enter the message: ";
cin >> msg;
cout << "Enter key: ";
cin >> key;
string encrypted = encrypt(msg, key);
cout << "Encrypted: " << encrypted << endl;
string decrypted = decrypt(encrypted, key);
cout << "Decrypted: " << decrypted << endl;
return 0;
}
string encrypt(string msg, int key) {
string encrypted = "";
for (char ch : msg) {
encrypted.push_back('a' + (ch - 'a' + key) % 26);
}
return encrypted;
}
string decrypt(string msg, int key) {
string decrypted = "";
for (char ch : msg) {
decrypted.push_back('a' + (ch - 'a' - key + 26) % 26); // Add 26 to ensure positive modulo
}
return decrypted;
}
Editor is loading...
Leave a Comment