-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar-cipher.cpp
98 lines (88 loc) · 3.4 KB
/
caesar-cipher.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "caesar-cipher.h"
void encryption(const string &plainAlpha) {
int key;
string message, cipherAlphabet, cipherText;
// input number of places to shift left
cout << "Enter your chosen cipher key. (integer, 1-25): ";
cin >> key;
// input validation
while (!cin || key > 25 || key < 1) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Error! Enter an integer between 1 and 25: ";
cin >> key;
}
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // discard \n
cout << "Enter the message to encrypt: "; // input message to encrypt
getline(cin, message);
// shift alphabet [key] letters to the left
cipherAlphabet = cipherAlpha(key, plainAlpha);
// encrypt the message using the cipher alphabet
cipherText = cipherMessage(message, key);
cout << "\nPlaintext: " << message << endl;
cout << "Plain alphabet: " << plainAlpha << "." << endl;
if (key == 13)
cout << "Cipher alphabet: " << cipherAlphabet << "; In ROT13." << endl;
else
cout << "Cipher alphabet: " << cipherAlphabet << "; Shifted [" << key << "] place(s) to the left." << endl;
cout << "Ciphertext: " << cipherText << endl << endl;
}
void decryption(const string &plainAlpha) {
int key;
string cipherText;
cout << "Enter your known cipher key. (integer, 1-25): ";
cin >> key;
// input validation
while (!cin || key > 25 || key < 1) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Error! Enter an integer between 1 and 25: ";
cin >> key;
}
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // discard \n
cout << "Enter the message to decrypt: "; // input message to decrypt
getline(cin, cipherText);
cout << "\nCiphertext: " << cipherText << endl;
cout << "Plain alphabet: " << plainAlpha << "." << endl;
if (key == 13)
cout << "Cipher alphabet: " << cipherAlpha(key, plainAlpha) << "; In ROT13." << endl;
else
cout << "Cipher alphabet: " << cipherAlpha(key, plainAlpha) << "; Shifted [" << key << "] place(s) to the left." << endl;
// display decrypted message
cout << "Plaintext: " << decrypt(cipherText, key) << endl << endl;
}
string cipherAlpha(int &key, const string &plainAlpha) {
string cipherAlpha;
for (char c : plainAlpha) {
cipherAlpha += static_cast<char>((c + key - 65) % 26 + 65);
}
return cipherAlpha;
}
string cipherMessage(const string &message, int &key) {
string cipherText;
for (char c : message) {
if (c >= 65 && c <= 90 || c >= 97 && c <= 122)
if (isupper(c)) {
cipherText += static_cast<char>((c + key - 65) % 26 + 65);
} else {
cipherText += static_cast<char>((c + key - 97) % 26 + 97);
}
else
cipherText += c;
}
return cipherText;
}
string decrypt(const string &cipherText, int &key) {
string plainText;
for (char c : cipherText) {
if (c >= 65 && c <= 90 || c >= 97 && c <= 122)
if (isupper(c)) {
plainText += static_cast<char>((c - key - 65 % 26 + 26) % 26 + 65);
} else {
plainText += static_cast<char>((c - key - 97 % 26 + 26) % 26 + 97);
}
else
plainText += c;
}
return plainText;
}