/
main.cpp
283 lines (276 loc) · 10.4 KB
/
main.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
Cipher program main file.
@file main.cpp
@author Devansh Singh
Chirag Tyagi
@brief The main file for compiling and running cipher program.
@date 28/02/2022
*/
#include <iostream>
#include <string>
#include "lib/cipher.hpp"
std::string CIPHER_ART = R"( ______ __ ______ __ __ ______ ______ )" "\n"
R"(/\ ___\ /\ \ /\ == \ /\ \_\ \ /\ ___\ /\ == \ )" "\n"
R"(\ \ \____ \ \ \ \ \ _-/ \ \ __ \ \ \ __\ \ \ __< )" "\n"
R"( \ \_____\ \ \_\ \ \_\ \ \_\ \_\ \ \_____\ \ \_\ \_\ )" "\n"
R"( \/_____/ \/_/ \/_/ \/_/\/_/ \/_____/ \/_/ /_/ )" "\n"
R"( )" "\n";
std::string MENU = R"(MENU)" "\n"
R"(----)" "\n"
R"(1. ASCII Code)" "\n"
R"(2. Atbash Cipher)" "\n"
R"(3. Binary Code)" "\n"
R"(4. Caesar Cipher)" "\n"
R"(5. Hex Code)" "\n"
R"(6. Morse Code)" "\n"
R"(7. NATO Phonetic Code)" "\n"
R"(8. Octal Code)" "\n"
R"(9. Vigenere Cipher)" "\n"
R"(10. Encrypt/Decrypt a file)" "\n"
R"(11. Exit)";
std::string FILE_MENU = R"(FILE MENU)" "\n"
R"(---------)" "\n"
R"(1. ASCII Code)" "\n"
R"(2. Atbash Cipher)" "\n"
R"(3. Binary Code)" "\n"
R"(4. Caesar Cipher)" "\n"
R"(5. Hex Code)" "\n"
R"(6. Morse Code)" "\n"
R"(7. NATO Phonetic Code)" "\n"
R"(8. Octal Code)" "\n"
R"(9. Vigenere Cipher)" "\n"
R"(10. Main Menu)";
/**
Encrypt/decrypt the data according to user input.
@param obj Reference of Cipher base class.
*/
void crypt(Cipher *obj) {
std::string option;
std::cout << "cipher> Encrypt/Decrypt: ";
std::cin >> option;
if(option == "encrypt" || option == "Encrypt" || option == "e" || option == "E"){
std::cout << "cipher> Encrypted data: " << obj->encrypt() << std::endl;
}else if(option == "decrypt" || option == "Decrypt" || option == "d" || option == "D"){
std::cout << "cipher> Decrypted data: " << obj->decrypt() << std::endl;
}else{
std::cout << "cipher> [!] Choose a correct option." << std::endl;
}
}
/**
Encrypt/decrypt a file according to user input.
@param obj Reference of Cipher base class.
*/
void cryptFile(Cipher *obj) {
std::string option, result;
std::cout << "(file) cipher> Encrypt/Decrypt: ";
std::cin >> option;
if(option == "encrypt" || option == "Encrypt" || option == "e" || option == "E"){
result = obj->encrypt();
if(result == "1") {
std::cout << "(file) cipher> [!] Unable to encrypt file." << std::endl;
}else {
std::cout << "(file) cipher> [+] File encrypted." << std::endl;
}
}else if(option == "decrypt" || option == "Decrypt" || option == "d" || option == "D"){
result = obj->decrypt();
if(result == "1") {
std::cout << "(file) cipher> [!] Unable to decrypt file." << std::endl;
}else {
std::cout << "(file) cipher> [+] File decrypted." << std::endl;
}
}else{
std::cout << "(file) cipher> [!] Choose a correct option." << std::endl;
}
}
int main(){
std::cout << CIPHER_ART << std::endl;
std::cout << MENU << std::endl;
while(true){
int option;
std::cout << "\ncipher> Enter option: ";
std::cin >> option;
std::cin.ignore();
switch(option){
case 1: {
std::string data;
std::cout << "cipher> Enter data: ";
getline(std::cin, data);
ASCIICode obj(data);
crypt(&obj);
break;
}
case 2: {
std::string data;
std::cout << "cipher> Enter data: ";
getline(std::cin, data);
AtbashCipher obj(data);
crypt(&obj);
break;
}
case 3: {
std::string data;
std::cout << "cipher> Enter data: ";
getline(std::cin, data);
BinaryCode obj(data);
crypt(&obj);
break;
}
case 4: {
std::string data;
unsigned int key;
std::cout << "cipher> Enter data: ";
getline(std::cin, data);
std::cout << "cipher> Enter key (offset): ";
std::cin >> key;
CaesarCipher obj(data, key);
crypt(&obj);
break;
}
case 5: {
std::string data;
std::cout << "cipher> Enter data: ";
getline(std::cin, data);
HexCode obj(data);
crypt(&obj);
break;
}
case 6: {
std::string data;
std::cout << "cipher> Enter data: ";
getline(std::cin, data);
MorseCode obj(data);
crypt(&obj);
break;
}
case 7: {
std::string data;
std::cout << "cipher> Enter data: ";
getline(std::cin, data);
NATOPhoneticCode obj(data);
crypt(&obj);
break;
}
case 8: {
std::string data;
std::cout << "cipher> Enter data: ";
getline(std::cin, data);
OctalCode obj(data);
crypt(&obj);
break;
}
case 9: {
std::string data, key;
std::cout << "cipher> Enter data: ";
getline(std::cin, data);
std::cout << "cipher> Enter key: ";
std::cin >> key;
VigenereCipher obj(data, key);
crypt(&obj);
break;
}
case 10: {
bool flag = true;
std::cout << std::endl << FILE_MENU << std::endl;
while(flag) {
int file_option;
std::cout << "\n(file) cipher> Enter option: ";
std::cin >> file_option;
std::cin.ignore();
switch(file_option){
case 1: {
std::string file_path;
std::cout << "(file) cipher> Enter file path: ";
getline(std::cin, file_path);
ASCIICode obj(file_path, true);
cryptFile(&obj);
break;
}
case 2: {
std::string file_path;
std::cout << "(file) cipher> Enter file path: ";
getline(std::cin, file_path);
AtbashCipher obj(file_path, true);
cryptFile(&obj);
break;
}
case 3: {
std::string file_path;
std::cout << "(file) cipher> Enter file path: ";
getline(std::cin, file_path);
BinaryCode obj(file_path, true);
cryptFile(&obj);
break;
}
case 4: {
std::string file_path;
unsigned int key;
std::cout << "(file) cipher> Enter file path: ";
getline(std::cin, file_path);
std::cout << "(file) cipher> Enter key (offset): ";
std::cin >> key;
CaesarCipher obj(file_path, key, true);
cryptFile(&obj);
break;
}
case 5: {
std::string file_path;
std::cout << "(file) cipher> Enter file path: ";
getline(std::cin, file_path);
HexCode obj(file_path, true);
cryptFile(&obj);
break;
}
case 6: {
std::string file_path, crypt;
std::cout << "(file) cipher> Enter file path: ";
getline(std::cin, file_path);
MorseCode obj(file_path, true);
cryptFile(&obj);
break;
}
case 7: {
std::string file_path;
std::cout << "(file) cipher> Enter file path: ";
getline(std::cin, file_path);
NATOPhoneticCode obj(file_path, true);
cryptFile(&obj);
break;
}
case 8: {
std::string file_path;
std::cout << "(file) cipher> Enter file path: ";
getline(std::cin, file_path);
OctalCode obj(file_path, true);
cryptFile(&obj);
break;
}
case 9: {
std::string file_path, key;
std::cout << "(file) cipher> Enter file path: ";
getline(std::cin, file_path);
std::cout << "(file) cipher> Enter key: ";
std::cin >> key;
VigenereCipher obj(file_path, key, true);
cryptFile(&obj);
break;
}
case 10:
flag = false;
break;
default:
std::cout << "(file) cipher> [!] Choose a correct option." << std::endl;
break;
}
}
break;
}
case 11:
std::cout << "cipher> [+] Terminating program." << std::endl;
exit(0);
default:
std::cout << "cipher> [!] Choose a correct option." << std::endl;
break;
}
}
return 0;
}