-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcaesar.cpp
More file actions
227 lines (205 loc) · 6.35 KB
/
caesar.cpp
File metadata and controls
227 lines (205 loc) · 6.35 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
void print_welcome_message() {
cout << " ██████ █████ ███████ ███████ █████ ██████ \n";
cout << "██ ██ ██ ██ ██ ██ ██ ██ ██ \n";
cout << "██ ███████ █████ ███████ ███████ ██████ \n";
cout << "██ ██ ██ ██ ██ ██ ██ ██ ██ \n";
cout << " ██████ ██ ██ ███████ ███████ ██ ██ ██ ██ ";
cout << "by 01!v3r\n" << endl;
cout << "Welcome to caesar!" << endl;
}
int getkey() {
int key = 0;
while (key <= 0 || key > 25) {
cout << "Enter your key (1-25): " << endl;
cin >> key;
if (cin.fail()) {
cout << "ERROR: You did not enter an integer." << endl;
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return key;
}
string encrypt(string text, int key) {
string result = "";
key %= 26; // only allow 0-25;
for (int i = 0; i < text.length(); i++) {
unsigned char ch = text[i] + key; // prevent buffer overflow (0 - 256)
if (isupper(text[i])) {
if (ch > 'Z') {
ch -= 26;
}
result += ch;
} else if (islower(text[i])) {
if (ch > 'z') {
ch -= 26;
}
result += ch;
} else {
result += text[i];
}
}
return result;
}
string decrypt(string text, int key) {
string result = "";
key %= 26; // only allow 0-25;
for (int i = 0; i < text.length(); i++) {
char ch = text[i] - key; // (-128 - 127)
if (isupper(text[i])) {
if (ch < 'A') {
ch += 26;
}
result += ch;
} else if (islower(text[i])) {
if (ch < 'a') {
ch += 26;
}
result += ch;
} else {
result += text[i];
}
}
return result;
}
void bruteforce(string text) {
cout << "key | OUTPUT" << endl;
for (int i = 1; i < 26; i++) {
string seperator = (i < 10) ? " | " : " | ";
cout << "----|";
for (int i = 0; i < 55; i++)
cout << "-";
cout << endl;
cout << i << seperator << decrypt(text, i) << endl;
}
}
void interactive_mode() {
char option;
while (option != 'e' || option != 'd' || option != 'b') {
int key = 0;
for (int i = 0; i < 40; i++)
cout << "-";
cout << endl
<< " e) encode\n d) decode\n b) bruteforce ( try any possible keys "
")\n Select your option (Ctrl+c to quit) > ";
cin >> option;
if (cin.fail()) {
cout << "ERROR: You did not enter an integer." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} else {
cin.clear();
if (cin.fail()) {
cout << "ERROR: You did not enter an integer." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (option == 'e') { // encrypt
cout << endl << "Enter some text to encrypt: " << endl;
string text;
getline(cin, text);
key = getkey();
string result = encrypt(text, key);
cout << result << endl;
} else if (option == 'd') { // decrypt
cout << endl << "Enter some text to decrypt: " << endl;
string text;
getline(cin, text);
key = getkey();
string result = decrypt(text, key);
cout << result << endl;
} else if (option == 'b') {
cout << endl << "Enter some text to bruteforce: " << endl;
string text;
getline(cin, text);
bruteforce(text);
} else {
cout << "invalid option" << endl;
cin.clear();
cin.sync();
}
}
}
}
}
void print_help() {
cout << "Caesar 1.0.0 (https://github.com/knhn1004/caesar)" << endl;
cout << "Usage: caesar [Options] {target string} (-k {key})" << endl;
cout << "OPTIONS: " << endl;
cout << " -h: help message" << endl;
cout << " -e: encrypt string" << endl;
cout << " -d: decrypt code" << endl;
cout << " -k: key (number to cypher)" << endl;
cout << " -b: brute force code" << endl;
cout << " -i: enter interactive mode" << endl;
cout << "EXAMPLES: " << endl;
cout << " caesar -e oliver -k 15" << endl;
cout << " caesar -d daxktg -k 15" << endl;
cout << " caesar -b daxktg" << endl;
cout << " caesar -b \"svcl Jyfwavnyhwof\\!\"" << endl;
cout << " (notice the usage of escape characters)" << endl;
}
int main(int argc, char *argv[]) {
print_welcome_message();
if (argc > 1) {
/* ------------------
* for comparison
* ---------------- */
string intera("-i"); // interactive
string encr("-e"); // encrypt
string decr("-d"); // decrypt
string brute("-b"); // bruteforce
string key("-k"); // key
if (intera.compare(argv[1]) == 0) { // option '-i'
interactive_mode();
} else if (encr.compare(argv[1]) == 0) { // option '-e'
if (argc < 4 || key.compare(argv[3]) != 0) { // check if '-k' is specified
print_help();
return 0;
}
try {
// fix argv getting wrong integer off of string
string result = encrypt(argv[2], stoi(argv[4]));
cout << result << endl;
} catch (int e) {
cerr << "invalid syntax" << endl;
print_help();
}
} else if (decr.compare(argv[1]) == 0) { // option '-d'
if (argc < 4 || key.compare(argv[3]) != 0) {
print_help();
return 0;
}
try {
string result = decrypt(argv[2], stoi(argv[4]));
cout << result << endl;
} catch (int e) {
cerr << "invalid syntax" << endl;
print_help();
}
} else if (brute.compare(argv[1]) == 0) { // option '-b'
if (argc < 3) {
print_help();
return 0;
}
try {
bruteforce(argv[2]);
} catch (int e) {
cerr << "invalid syntax" << endl;
print_help();
}
} else {
print_help();
}
} else {
print_help();
}
return 0;
}