-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
393 lines (319 loc) · 13.6 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include "resource.h"
#include "include/cryptools.h"
#include "include/zedwood/md5.h"
#include "include/zedwood/sha512.h"
#define MCHAR 4096
HINSTANCE hInst;
bool cmdClean = true;
HWND HashesComboBox;
enum cryptions {
CXOR,
CROT13,
CTOBASE64,
CFROMBASE64,
CTOCAESAR,
CFROMCAESAR,
CTOVIGENERE,
CFROMVIGENERE,
CTOSAFEVIGE,
CFROMSAFEVIGE
};
bool vige_alpha_checked = false;
void fillHashComboBox();
void ProcessHash(HWND parent, int input, int output);
void ProcessKeyExtension(HWND parent, int input, int key, int output);
void ProcessEncryption(cryptions method, HWND parent, int input,int key, int output);
void ProcessEncryptionS(cryptions method1, cryptions method2, HWND parent, int input, int key, int output);
std::string ProcessText(std::string text, std::string dict);
void stringSwitch(std::string & inputStd, std::string & keyStd, char * keySTR, cryptions method);
BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
{
HashesComboBox = GetDlgItem(hwndDlg, ID_HASHES_COMBOBOX);
fillHashComboBox();
}
return TRUE;
case WM_CLOSE:
{
EndDialog(hwndDlg, 0);
}
return TRUE;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
//Ciphers buttons
case ID_CIPHERS_DLGXOR_BUTTON:
DialogBox(hInst, MAKEINTRESOURCE(DLG_XOR), NULL, (DLGPROC)DlgMain);
break;
case ID_CIPHERS_DLGROT13_BUTTON:
DialogBox(hInst, MAKEINTRESOURCE(DLG_ROT13), NULL, (DLGPROC)DlgMain);
break;
case ID_CIPHERS_DLGB64_BUTTON:
DialogBox(hInst, MAKEINTRESOURCE(DLG_B64), NULL, (DLGPROC)DlgMain);
break;
case ID_CIPHERS_CAESAR_BUTTON:
DialogBox(hInst, MAKEINTRESOURCE(DLG_CAESAR), NULL, (DLGPROC)DlgMain);
break;
case ID_CIPHERS_VIGENERE_BUTTON:
DialogBox(hInst, MAKEINTRESOURCE(DLG_VIGENERE), NULL, (DLGPROC)DlgMain);
break;
case ID_CIPHERS_SAVEVIGE_BUTTON:
DialogBox(hInst, MAKEINTRESOURCE(DLG_SAFEVIGE), NULL, (DLGPROC)DlgMain);
break;
//Others buttons
case ID_OTHERS_HASHES_BUTTON:
DialogBox(hInst, MAKEINTRESOURCE(DLG_HASHES), NULL, (DLGPROC)DlgMain);
break;
case ID_OTHERS_SVB64_BUTTON:
DialogBox(hInst, MAKEINTRESOURCE(DLG_SVB64), NULL, (DLGPROC)DlgMain);
break;
//Check boxes handling
case ID_VIGENERE_ALPHABET_CHECK:
if (SendDlgItemMessage(hwndDlg, ID_VIGENERE_ALPHABET_CHECK, BM_GETCHECK, 0, 0))
vige_alpha_checked = true;
else
vige_alpha_checked = false;
break;
//Combo box handling
case ID_HASHES_COMBOBOX:
if (HIWORD(wParam) == CBN_SELCHANGE) {
ProcessHash(hwndDlg, ID_HASHES_INPUT_TEXT, ID_HASHES_OUTPUT_TEXT);
}
break;
//Hash auto update
case ID_HASHES_INPUT_TEXT:
if (HIWORD(wParam) == EN_CHANGE)
ProcessHash(hwndDlg, ID_HASHES_INPUT_TEXT, ID_HASHES_OUTPUT_TEXT);
break;
case ID_SAFEVIGE_INPUT_TEXT:
case ID_SAFEVIGE_KEY_LINE:
if (HIWORD(wParam) == EN_CHANGE)
ProcessKeyExtension(hwndDlg, ID_SAFEVIGE_INPUT_TEXT, ID_SAFEVIGE_KEY_LINE, ID_SAFEVIGE_LONGKEY_TEXT);
break;
//Dialogs buttons
case ID_XOR_APPLY_PUSHBUTTON:
ProcessEncryption(CXOR, hwndDlg, ID_XOR_INPUT_TEXT, ID_XOR_KEY_LINE, ID_XOR_OUTPUT_TEXT);
break;
case ID_ROT13_APPLY_PUSHBUTTON:
ProcessEncryption(CROT13, hwndDlg, ID_ROT13_INPUT_TEXT, 0, ID_ROT13_OUTPUT_TEXT);
break;
case ID_BASE64_ENCODE_BUTTON:
ProcessEncryption(CTOBASE64, hwndDlg, ID_BASE64_INPUT_TEXT, 0, ID_BASE64_OUTPUT_TEXT);
break;
case ID_BASE64_DECODE_BUTTON:
ProcessEncryption(CFROMBASE64, hwndDlg, ID_BASE64_INPUT_TEXT, 0, ID_BASE64_OUTPUT_TEXT);
break;
case ID_CAESAR_ENCRYPT_BUTTON:
ProcessEncryption(CTOCAESAR, hwndDlg, ID_CAESAR_INPUT_TEXT, ID_CAESAR_KEY_LINE, ID_CAESAR_OUTPUT_TEXT);
break;
case ID_CAESAR_DECRYPT_BUTTON:
ProcessEncryption(CFROMCAESAR, hwndDlg, ID_CAESAR_INPUT_TEXT, ID_CAESAR_KEY_LINE, ID_CAESAR_OUTPUT_TEXT);
break;
case ID_VIGENERE_ENCRYPT_BUTTON:
ProcessEncryption(CTOVIGENERE, hwndDlg, ID_VIGENERE_INPUT_TEXT, ID_VIGENERE_KEY_LINE, ID_VIGENERE_OUTPUT_TEXT);
break;
case ID_VIGENERE_DECRYPT_BUTTON:
ProcessEncryption(CFROMVIGENERE, hwndDlg, ID_VIGENERE_INPUT_TEXT, ID_VIGENERE_KEY_LINE, ID_VIGENERE_OUTPUT_TEXT);
break;
case ID_SAFEVIGE_ENCRYPT_BUTTON:
ProcessEncryption(CTOSAFEVIGE, hwndDlg, ID_SAFEVIGE_INPUT_TEXT, ID_SAFEVIGE_LONGKEY_TEXT, ID_SAFEVIGE_OUTPUT_TEXT);
break;
case ID_SAFEVIGE_DECRYPT_BUTTON:
ProcessEncryption(CFROMSAFEVIGE, hwndDlg, ID_SAFEVIGE_INPUT_TEXT, ID_SAFEVIGE_LONGKEY_TEXT, ID_SAFEVIGE_OUTPUT_TEXT);
break;
//Multiple encryptions buttons
case ID_SVB64_ENCRYPT_BUTTON:
ProcessEncryptionS(CTOSAFEVIGE, CTOBASE64, hwndDlg, ID_SVB64_INPUT_TEXT, ID_SVB64_KEY_LINE, ID_SVB64_OUTPUT_TEXT);
break;
case ID_SVB64_DECRYPT_BUTTON:
ProcessEncryptionS(CFROMBASE64, CFROMSAFEVIGE, hwndDlg, ID_SVB64_INPUT_TEXT, ID_SVB64_KEY_LINE, ID_SVB64_OUTPUT_TEXT);
break;
}
}
return TRUE;
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hInst=hInstance;
InitCommonControls();
return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}
void ProcessEncryption(cryptions method, HWND parent, int input, int key, int output)
{
if (cmdClean == false)
std::cout << "--------------------------------------\r\n\r\n";
char inputSTR[MCHAR], keySTR[MCHAR];
GetDlgItemText(parent, input, inputSTR, MCHAR);
if (key != 0)
GetDlgItemText(parent, key, keySTR, MCHAR);
std::string inputStd(inputSTR), keyStd(keySTR), outputStd;
std::cout << "================\r\nStarting encryption\r\n================" << std::endl;
std::cout << "Input STD: " << inputStd << std::endl;
if (key != 0)
std::cout << "Key STD: " << keyStd << std::endl;
std::cout << std::endl;
stringSwitch(inputStd, keyStd, keySTR, method);
std::cout << std::endl << "Output STD: " << inputStd << std::endl << std::endl;
const char* outputSTR = inputStd.c_str();
SetDlgItemText(parent, output, outputSTR);
cmdClean = false;
return;
}
std::string ProcessText(std::string text, std::string dict)
{
std::string output;
unsigned int textlen = text.length();
for (unsigned int i=0; i<textlen; ++i) {
if (CrypTools::containsWhat(dict, text[i]) == true) {
output += text[i];
}
}
return output;
}
void fillHashComboBox()
{
SendMessage(HashesComboBox, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>((LPCTSTR)"MD5"));
SendMessage(HashesComboBox, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>((LPCTSTR)"SHA256"));
SendMessage(HashesComboBox, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>((LPCTSTR)"SHA512"));
SendMessage(HashesComboBox, CB_SELECTSTRING, 0, reinterpret_cast<LPARAM>((LPCTSTR)"SHA256"));
}
void ProcessHash(HWND parent, int input, int output)
{
char INCHAR[MCHAR];
GetDlgItemText(parent, input, INCHAR, MCHAR);
unsigned int currentIndex =
SendMessage(HashesComboBox, CB_GETCURSEL, 0, 0);
std::string INSTD(INCHAR);
std::string OUTSTD;
switch (currentIndex)
{
case 0:
OUTSTD = md5(INSTD);
break;
case 1:
OUTSTD = sha256(INSTD);
break;
case 2:
OUTSTD = sha512(INSTD);
break;
default:
OUTSTD = "Invalid selection";
break;
}
SetDlgItemText(parent, output, OUTSTD.c_str());
}
void ProcessKeyExtension(HWND parent, int input, int key, int output)
{
char INCHAR[MCHAR], KEYCHAR[MCHAR];
GetDlgItemText(parent, input, INCHAR, MCHAR);
GetDlgItemText(parent, key, KEYCHAR, MCHAR);
std::string INSTD(INCHAR), KEYSTD(KEYCHAR);
std::string OUTSTD = CrypTools::generateKey(KEYSTD, INSTD.length());
SetDlgItemText(parent, output, OUTSTD.c_str());
}
void ProcessEncryptionS(cryptions method1, cryptions method2, HWND parent, int input, int key, int output)
{
if (cmdClean == false)
std::cout << "--------------------------------------\r\n\r\n";
char inputSTR[MCHAR], keySTR[MCHAR];
GetDlgItemText(parent, input, inputSTR, MCHAR);
if (key != 0)
GetDlgItemText(parent, key, keySTR, MCHAR);
std::string inputStd(inputSTR), keyStd(keySTR);
std::cout << "================\r\nStarting encryption\r\n================" << std::endl;
std::cout << "Input STD: " << inputStd << std::endl;
if (key != 0)
std::cout << "Key STD: " << keyStd << std::endl;
std::cout << std::endl;
stringSwitch(inputStd, keyStd, keySTR, method1);
stringSwitch(inputStd, keyStd, keySTR, method2);
std::cout << std::endl << "Output STD: " << inputStd << std::endl << std::endl;
const char* outputSTR = inputStd.c_str();
SetDlgItemText(parent, output, outputSTR);
cmdClean = false;
return;
}
void stringSwitch(std::string & inputStd, std::string & keyStd, char * keySTR, cryptions method)
{
switch (method)
{
case CXOR:
std::cout << "================\r\nApplying XOR crypt\r\n================" << std::endl;
inputStd = CrypTools::XOR(inputStd, keyStd);
break;
case CROT13:
std::cout << "================\r\nProcessing text\r\n================" << std::endl;
inputStd = ProcessText(inputStd, "abcdefghijklmnopqrstuvwxyz");
std::cout << "Done" << std::endl;
std::cout << "================\r\nApplying ROT13 crypt\r\n================" << std::endl;
inputStd = CrypTools::rot13(inputStd);
break;
case CTOBASE64:
std::cout << "================\r\nEncoding to base64\r\n================" << std::endl;
inputStd = CrypTools::toBase64(inputStd);
break;
case CFROMBASE64:
std::cout << "================\r\nDecoding from base64\r\n================" << std::endl;
inputStd = CrypTools::fromBase64(inputStd);
break;
case CTOCAESAR:
std::cout << "================\r\nProcessing text\r\n================" << std::endl;
inputStd = ProcessText(inputStd, "abcdefghijklmnopqrstuvwxyz");
std::cout << "Done" << std::endl;
std::cout << "================\r\nApplying Caesar encryption\r\n================" << std::endl;
inputStd = CrypTools::caesarEncrypt(atoi(keySTR), inputStd, Types::LowercaseAlphabet);
break;
case CFROMCAESAR:
std::cout << "================\r\nProcessing text\r\n================" << std::endl;
inputStd = ProcessText(inputStd, "abcdefghijklmnopqrstuvwxyz");
std::cout << "Done" << std::endl;
std::cout << "================\r\nApplying Caesar decryption\r\n================" << std::endl;
inputStd = CrypTools::caesarEncrypt(26-atoi(keySTR), inputStd, Types::LowercaseAlphabet);
break;
case CTOVIGENERE:
if (vige_alpha_checked == true) {
std::cout << "================\r\nProcessing text\r\n================" << std::endl;
inputStd = ProcessText(inputStd, "abcdefghijklmnopqrstuvwxyz");
std::cout << "Done" << std::endl;
std::cout << "================\r\nApplying Vigenère encryption\r\n================" << std::endl;
inputStd = CrypTools::vigenereAlphaOnly(inputStd, keyStd, 1);
}
else {
std::cout << "================\r\nApplying Vigenère encryption\r\n================" << std::endl;
inputStd = CrypTools::vigenereEncrypt(inputStd, keyStd);
}
break;
case CFROMVIGENERE:
if (vige_alpha_checked == true) {
std::cout << "================\r\nProcessing text\r\n================" << std::endl;
inputStd = ProcessText(inputStd, "abcdefghijklmnopqrstuvwxyz");
std::cout << "Done" << std::endl;
std::cout << "================\r\nApplying Vigenère decryption\r\n================" << std::endl;
inputStd = CrypTools::vigenereAlphaOnly(inputStd, keyStd, -1);
}
else {
std::cout << "================\r\nApplying Vigenère decryption\r\n================" << std::endl;
inputStd = CrypTools::vigenereDecrypt(inputStd, keyStd);
}
break;
case CTOSAFEVIGE:
std::cout << "================\r\nApplying safe Vigenère encryption\r\n================" << std::endl;
inputStd = CrypTools::vigenereEncrypt(inputStd, keyStd);
break;
case CFROMSAFEVIGE:
std::cout << "================\r\nApplying safe Vigenère decryption\r\n================" << std::endl;
inputStd = CrypTools::vigenereDecrypt(inputStd, keyStd);
break;
}
}