-
Notifications
You must be signed in to change notification settings - Fork 1
/
ROT13.cpp
37 lines (29 loc) · 921 Bytes
/
ROT13.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
#include <iostream>
#include <string>
using namespace std;
string encryptROT13(string message) {
string encMessage = "";
// character == c
for (char c : message) {
if (isalpha(c)) {
char base = islower(c) ? 'a' : 'A';
c = ((c - base + 13) % 26) + base;
}
encMessage += c;
}
return encMessage;
}
string decryptROT13(string message) {
return encryptROT13(message);
// ROT13 là phép mã hóa tự đối xứng, nên để giải mã, chỉ cần mã hóa lại
}
int main() {
string message;
cout << "Enter the message to be encrypted: ";
getline(cin, message);
string enMessage = encryptROT13(message);
cout << "Encrypted message: " << encMessage << endl;
string decMessage = decryptROT13(encMessage);
cout << "Decrypted message: " << decMessage << endl;
return 0;
}