-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaesarBruteForce.cpp
48 lines (33 loc) · 1017 Bytes
/
CaesarBruteForce.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
#include <bits/stdc++.h>
using namespace std;
string caesar(string text, int shift) {
string result = "";
for (int i = 0; i < text.length(); ++i) {
// eliminate white spaces
if(text[i] != ' ') {
if (isupper(text[i])) {
result += char(int(text[i] + shift - 65) % 26 + 65);
} else {
result += char(int(text[i] + shift - 97) % 26 + 97);
}
}
}
return result;
}
int main() {
string text;
cout<<"Plain text: ";
getline(cin, text);
// transform string to upper if necessary
transform(text.begin(), text.end(), text.begin(), ::toupper);
int shift;
cout<<"Shift: ";
cin>>shift;
string cipher = caesar(text, shift);
cout<<"Cipher text: "<<cipher<<endl;
for (int i = 1; i < 25; ++i)
{
cout<<"Shift "<<(i > 9 ? "" : "0")<<i<<": "<<caesar(cihper, 26 - i)<<endl;
}
return 0;
}