-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesar.cpp
86 lines (76 loc) · 2.25 KB
/
caesar.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
#include <iostream>
#include <fstream>
#include <algorithm>
#include <sstream>
#include <iomanip>
using namespace std;
string rot(int n, string str) {
if (n > 26 || n < -26)
return "\nRotation integer out of range!\nPlease choose a number between -26 and 26!\n";
if (n < 0)
n += 26;
string alph = "abcdefghijklmnopqrstuvwxyz";
string ciph = alph;
rotate(ciph.begin(), ciph.begin() + n, ciph.end());
string upperAlph = alph;
string upperCiph = ciph;
transform(upperAlph.begin(), upperAlph.end(), upperAlph.begin(), ::toupper);
transform(upperCiph.begin(), upperCiph.end(), upperCiph.begin(), ::toupper); // or alph.upcase in Ruby or alph.upper() in Python...
unsigned int i = 0;
unsigned int j = 0;
while (i < str.length()) {
if (alph.find(str[i]) != std::string::npos || upperAlph.find(str[i]) != std::string::npos) {
if (str[i] == alph[j]) {
str[i] = ciph[j];
i++;
j = 0;
}
else if (str[i] == upperAlph[j]) {
str[i] = upperCiph[j];
i++;
j = 0;
}
else {
j++;
}
}
else {
i++;
}
}
return str;
}
int main(int argc, char** argv) {
if (argc < 4) {
cout << "Please supply a valid number of arguments." << endl;
return 1;
}
string content;
if (string(argv[2]) == "text") {
content = string(argv[3]);
}
else if (string(argv[2]) == "file") {
string fileName = string(argv[3]);
ifstream ifs(fileName);
content.assign(
istreambuf_iterator<char>(ifs),
istreambuf_iterator<char>()
);
}
else {
cout << "Please choose a way of retrieving the input (text or file)." << endl;
return 1;
}
string strRotation = argv[1];
if (strRotation == "bruteforce" || strRotation == "brute" || strRotation == "force") {
for (unsigned int i = 25; i >= 1; i--) {
stringstream list;
list << setfill('0') << setw(2) << 26 - i; // Adds padding with '0's, e.g. {5 => 05, 14 => 14} etc...
cout << list.str() << ": " << rot(i, content) << endl; // 26 - i to reverse the numbers because brute force decrypts, showing the encryption number as opposed to the decryption number
}
return 0;
}
int rotation = atoi(strRotation.c_str());
cout << rot(rotation, content);
return 0;
}