-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.cpp
More file actions
263 lines (217 loc) · 4.51 KB
/
Command.cpp
File metadata and controls
263 lines (217 loc) · 4.51 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
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
#include "include/RSA.h"
#include "include/IO.h"
#include "include/Command.h"
#include "include/Converter.h"
using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::fstream;
bool isValidCommand(int command, int commandCount)
{
return command > 0 && command <= commandCount;
}
int printMainMenu()
{
int command = 0;
do {
system("cls");
cout << "==== Welcome to RSA Cryptosystem ====\n";
cout << "1. Generate or insert keys\n";
cout << "2. Encrypt or decrypt\n";
cout << "3. Exit\n";
cout << "Select key type: ";
cin >> command;
system("cls");
} while (!isValidCommand(command, 3));
return command;
}
int printKeyTypeMenu()
{
int type = 0;
do {
cout << "==== KEY TYPE ====\n";
cout << "1. Generate keys\n";
cout << "2. Use your own keys\n";
cout << "Select key type: ";
cin >> type;
} while (!isValidCommand(type, 2));
return type;
}
RSA handleKeyTypeMenu(int type)
{
switch (type)
{
case 1:
{
int keySize = handleKeySizeMenu(printKeySizeMenu());
int keyBase = handleKeyBaseMenu(printKeyBaseMenu());
int exportMethod = handleExportMethodMenu(printExportMethodMenu());
uint32_t rsaByteCount = BigInt::maxByteCount = keySize / 8;
RSA rsa(rsaByteCount, keyBase);
io.exportKeys(rsa, exportMethod);
return rsa;
}
case 2:
{
int keyBase = handleKeyBaseMenu(printKeyBaseMenu());
auto keys = io.insertKeys(keyBase);
// Todo: thay thế các cú pháp dùng tie bằng cú pháp structured bindings (C++17)
auto [n, e, d] = converter.toRSAKeys(keys, keyBase);
//! Bước xác định số byte tối đa này rất quan trọng, sẽ đảm bảo chương trình chạy đúng
BigInt::maxByteCount = getMaxByteCount(n.byteCount, d.byteCount);
RSA rsa(n, e, d, keyBase);
return rsa;
}
default:
return RSA();
break;
}
};
int printKeySizeMenu()
{
int option = 0;
do {
cout << "\n==== KEY SIZE ====\n";
cout << "1. 128 bit (super recommended)\n";
cout << "2. 256 bit (recommended)\n";
cout << "3. 512 bit\n";
cout << "4. 1024 bit\n";
cout << "5. 2048 bit\n";
cout << "Select key size: ";
cin >> option;
} while (!isValidCommand(option, 5));
return option;
}
int handleKeySizeMenu(int option)
{
int keySize = 0;
if (option == 1)
keySize = 128;
else if (option == 2)
keySize = 256;
else if (option == 3)
keySize = 512;
else if (option == 4)
keySize = 1024;
else if (option == 5)
keySize = 2048;
else
keySize = 0;
return keySize;
}
int printKeyBaseMenu()
{
int option = 0;
do {
cout << "\n==== KEY BASE ====\n";
cout << "1. Decimal\n";
cout << "2. Binary (recommended)\n";
cout << "Select key base: ";
cin >> option;
system("cls");
} while (!isValidCommand(option, 2));
return option;
}
int handleKeyBaseMenu(int option)
{
int keyBase = option == 1 ? BigIntBase::BASE_10 : BigIntBase::BASE_2;
return keyBase;
}
int printExportMethodMenu()
{
int option = 0;
do {
cout << "\n==== EXPORT METHOD ====\n";
cout << "1. Export keys to keys.txt\n";
cout << "2. Display keys on console\n";
cout << "3. Both\n";
cout << "Select export method: ";
cin >> option;
system("cls");
} while (!isValidCommand(option, 3));
return option;
}
int handleExportMethodMenu(int option)
{
int method = 0;
switch (option)
{
case 1:
method = ExportMethod::FILE;
break;
case 2:
method = ExportMethod::CONSOLE;
break;
case 3:
method = ExportMethod::BOTH;
break;
default:
break;
}
return method;
}
int printOperationMenu()
{
int operation = 0;
do {
cout << "==== OPERATION ====\n";
cout << "1. Encrypt\n";
cout << "2. Decrypt\n";
cout << "Select operation: ";
cin >> operation;
} while (!isValidCommand(operation, 2));
return operation;
}
void handleOperationMenu(RSA rsa, int option)
{
switch (option)
{
case 1:
{
string plaintText, cipherText;
tie(plaintText, cipherText) = io.inputFilesForEncryption();
rsa.encryptFile(plaintText, cipherText);
break;
}
case 2:
{
string cipherText, decryptedText;
tie(cipherText, decryptedText) = io.inputFilesForDecryption();
rsa.decryptFile(cipherText, decryptedText);
break;
}
default:
break;
}
}
void Command::run()
{
io.clearFile("resources/log.txt");
io.clearFile("resources/output.txt");
bool exit = false;
RSA rsa;
do {
int command = printMainMenu();
switch (command)
{
case 1:
{
rsa = handleKeyTypeMenu(printKeyTypeMenu());
}
break;
case 2:
{
handleOperationMenu(rsa, printOperationMenu());
}
break;
case 3:
{
exit = true;
break;
}
default:
break;
}
} while (exit == false);
}