-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmono-alphabetic.cpp
46 lines (42 loc) · 1.14 KB
/
mono-alphabetic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
using namespace std;
string encrypt(string message, string key) {
string ciphertext = "";
for (char c : message) {
if (isalpha(c)) {
char newChar = key[tolower(c) - 'a'];
if (isupper(c)) {
newChar = toupper(newChar);
}
ciphertext += newChar;
} else {
ciphertext += c;
}
}
return ciphertext;
}
string decrypt(string ciphertext, string key) {
string message = "";
for (char c : ciphertext) {
if (isalpha(c)) {
char newChar = 'a' + key.find(tolower(c));
if (isupper(c)) {
newChar = toupper(newChar);
}
message += newChar;
} else {
message += c;
}
}
return message;
}
int main() {
string message = "hello world";
string key = "qwertyuiopasdfghjklzxcvbnm";
string ciphertext = encrypt(message, key);
cout << "Ciphertext: " << ciphertext << endl;
string decryptedMessage = decrypt(ciphertext, key);
cout << "Decrypted message: " << decryptedMessage << endl;
return 0;
}