-
Notifications
You must be signed in to change notification settings - Fork 0
/
AES_LibExample.cpp
485 lines (475 loc) · 18.2 KB
/
AES_LibExample.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// AES_Implementation.cpp : Defines the entry point for the application.
//
#include "framework.h"
#include "AES_LibExample.h"
#include "..\\AES_Library\\AES_Library.h"
#pragma comment (lib, "..\\AES_Library\\Debug\\AES_Library.lib")
// Global Variables:
HINSTANCE hInst;
BYTE* DataBuffer;
BOOL ModifiedFlag;
WCHAR sFname[MAX_PATH];
short AEStype, KeySize, KeyType;
DWORD dwFsize, dwBufsize;
BYTE Key[AES_KEYSIZE256] = { 0 };
BYTE IV[AES_BLOCKSIZE] = { 0 };
// Forward declarations of functions included in this code module:
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow);
INT_PTR CALLBACK MainDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
BOOL OpenSaveGetFileName(HWND hwnd, LPCWSTR filter, LPCWSTR title, UCHAR selection, LPWSTR filepathname, LPWSTR filename);
DWORD GetSrcBufSize(LPCWSTR pssrcfname);
BOOL ReadFromFile(LPCWSTR pssrcfname, PBYTE psrcbuf, int ibufsize);
BOOL Write2File(LPCWSTR psdestfname, PBYTE pdestbuf, int idestsize);
void UpdateKeyCounter(HWND hDlg);
void GenerateSimpleInitializationVector(BYTE* IV);
void GenerateSimpleKey(BYTE* Key);
/*-------------------------------------------------------------------------------
GenerateSimpleKey
Output: Key
-------------------------------------------------------------------------------*/
void GenerateSimpleKey(BYTE* Key)
{
SYSTEMTIME st;
int random = rand();
GetSystemTime(&st);
st.wYear += st.wMilliseconds;
st.wMonth += random;
memcpy(Key, &st, 16);
if(KeySize != AES_KEYSIZE128)
{
random = rand();
memcpy(Key + 16, &random, 4);
random = rand();
memcpy(Key + 20, &random, 4);
random = rand();
memcpy(Key + 24, &random, 4);
random = rand();
memcpy(Key + 28, &random, 4);
}
}
/*-------------------------------------------------------------------------------
GenerateSimpleInitializationVector
Output: Iv
-------------------------------------------------------------------------------*/
void GenerateSimpleInitializationVector(BYTE* IV)
{
SYSTEMTIME st;
GetSystemTime(&st);
memcpy(IV, &st, 16);
}
/*-------------------------------------------------------------------------------
RunEncrytion
Input: KeySize, Func, Mode
Output: Iv
-------------------------------------------------------------------------------*/
void RunEncryption(int KeySize, int Func, short Mode, BYTE* Iv)
{
switch (Mode)
{
case AES_MODECBC:
case AES_MODEECB:
AES_ECBmodeCBCmodeEncrypt(Key, KeySize, DataBuffer, dwBufsize, Iv, Mode);
break;
case AES_MODECTR:
case AES_MODEOFB:
dwBufsize = dwFsize;
AES_CTRmodeOFBmodeEncryptDecrypt(Key, KeySize, DataBuffer, dwBufsize, Iv, Mode);
break;
case AES_MODECFB:
dwBufsize = dwFsize;
AES_CFBmodeEncryptDecrypt(Key, KeySize, DataBuffer, dwBufsize, Iv, Func);
break;
}
return;
}
/*-------------------------------------------------------------------------------
RunDecryption
Input: KeySize, Func, Mode
Output: Iv
-------------------------------------------------------------------------------*/
void RunDecryption(int KeySize, int Func, short Mode, BYTE* Iv)
{
switch (Mode)
{
case AES_MODECBC:
case AES_MODEECB:
AES_ECBmodeCBCmodeDecrypt(Key, KeySize, DataBuffer, dwBufsize, Iv, Mode);
break;
case AES_MODECTR:
case AES_MODEOFB:
dwBufsize = dwFsize;
AES_CTRmodeOFBmodeEncryptDecrypt(Key, KeySize, DataBuffer, dwBufsize, Iv, Mode);
break;
case AES_MODECFB:
dwBufsize = dwFsize;
AES_CFBmodeEncryptDecrypt(Key, KeySize, DataBuffer, dwBufsize, Iv, Func);
break;
}
return;
}
/*-------------------------------------------------------------------------------
HexFromNumber
Input: Input
Output: Output
-------------------------------------------------------------------------------*/
void HexFromNumber(WCHAR* Output, BYTE* Input, int Size)
{
WCHAR t[3];
for (int i = 0; i < Size; i++)
{
StringCchPrintf(t, 3, L"%02X", Input[i]);
Output[i * 2] = t[0];
Output[i * 2 + 1] = t[1];
}
Output[Size * 2] = '\0';
return;
}
/*-------------------------------------------------------------------------------
HexFromNumber
Input: Input, Length
Output: Key
-------------------------------------------------------------------------------*/
void KeyFromHex(WCHAR* Input, BYTE* Key, int Length)
{
WCHAR t[2];
for (int i = 0; i < Length; i += 2)
{
t[0] = Input[i];
t[1] = Input[i + 1];
UINT tmp = wcstoul(t, NULL, 16);
UCHAR x = tmp;
Key[i / 2] = x;
}
return;
}
/*-------------------------------------------------------------------------------
UpdateKeyCounter
Input: hDlg
Globals: KeyType, KeySize
-------------------------------------------------------------------------------*/
void UpdateKeyCounter(HWND hDlg)
{
int length;
WCHAR stmp[100];
length = Edit_GetTextLength(GetDlgItem(hDlg, IDC_EDIT1));
StringCchPrintf(stmp, 10, L"%d / %d", length, KeyType * KeySize);
Static_SetText(GetDlgItem(hDlg, IDC_COUNTER), stmp);
return;
}
/*-------------------------------------------------------------------------------
EnableDisableButtons
Input: hDlg, Status
-------------------------------------------------------------------------------*/
void EnableDisableButtons(HWND hDlg, BOOL Status)
{
Button_Enable(GetDlgItem(hDlg, IDC_BUTTONFSAVE), Status);
Button_Enable(GetDlgItem(hDlg, IDC_BUTTONENCRYPT), Status);
Button_Enable(GetDlgItem(hDlg, IDC_BUTTONDECRYPT), Status);
return;
}
/*-------------------------------------------------------------------------------
SetKeySize
Input: hDlg, Type, Keysize
Global: KeySize, KeyType
-------------------------------------------------------------------------------*/
void SetKeySize(HWND hDlg, int Type, short Keysize)
{
KeySize = Keysize;
Edit_LimitText(GetDlgItem(hDlg, IDC_EDIT1), KeyType * KeySize);
Button_SetCheck(GetDlgItem(hDlg, IDC_RADIO128), FALSE);
Button_SetCheck(GetDlgItem(hDlg, IDC_RADIO192), FALSE);
Button_SetCheck(GetDlgItem(hDlg, IDC_RADIO256), FALSE);
Button_SetCheck(GetDlgItem(hDlg, Type), TRUE);
Static_SetText(GetDlgItem(hDlg, IDC_EDIT1), L"");
UpdateKeyCounter(hDlg);
return;
}
/*-------------------------------------------------------------------------------
SetAEStype
Input: hDlg, Type
Global: AEStype
-------------------------------------------------------------------------------*/
void SetAEStype(HWND hDlg, int Type)
{
// uncheck all then check selected one
Button_SetCheck(GetDlgItem(hDlg, IDC_RADIOECB), FALSE);
Button_SetCheck(GetDlgItem(hDlg, IDC_RADIOCBC), FALSE);
Button_SetCheck(GetDlgItem(hDlg, IDC_RADIOCTR), FALSE);
Button_SetCheck(GetDlgItem(hDlg, IDC_RADIOOFB), FALSE);
Button_SetCheck(GetDlgItem(hDlg, IDC_RADIOCFB), FALSE);
Button_SetCheck(GetDlgItem(hDlg, Type), TRUE);
if (Type == IDC_RADIOECB)
Button_Enable(GetDlgItem(hDlg, IDC_EDIT2), FALSE);
else
Button_Enable(GetDlgItem(hDlg, IDC_EDIT2), TRUE);
AEStype = Type - IDC_RADIOECB;
return;
}
/*-----------------------------------------------------------------------------------------------
About
Message handler for About dialog box.
-----------------------------------------------------------------------------------------------*/
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
/*-----------------------------------------------------------------------------------------------
MainDialog
Message handler for main dialog box.
-----------------------------------------------------------------------------------------------*/
INT_PTR CALLBACK MainDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
int length;
WCHAR stmp[MAX_PATH];
HWND hwndOwner;
RECT rc, rcDlg, rcOwner;
switch (message)
{
case WM_INITDIALOG:
SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(hInst, MAKEINTRESOURCE(IDI_AESIMPLEMENTATION)));
SendMessage(hDlg, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(hInst, MAKEINTRESOURCE(IDI_SMALL)));
Button_SetCheck(GetDlgItem(hDlg, IDC_RADIO128), TRUE);
Button_SetCheck(GetDlgItem(hDlg, IDC_RADIOASCII), TRUE);
Button_SetCheck(GetDlgItem(hDlg, IDC_RADIOECB), TRUE);
Edit_LimitText(GetDlgItem(hDlg, IDC_EDIT1), 16);
Edit_LimitText(GetDlgItem(hDlg, IDC_EDIT2), 32);
EnableDisableButtons(hDlg, FALSE);
AEStype = AES_MODEECB;
KeySize = AES_KEYSIZE128;
KeyType = AES_KEYASCII;
hwndOwner = GetDesktopWindow();
GetWindowRect(hwndOwner, &rcOwner);
GetWindowRect(hDlg, &rcDlg);
CopyRect(&rc, &rcOwner);
OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
OffsetRect(&rc, -rc.left, -rc.top);
OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
SetWindowPos(hDlg, HWND_TOP, rcOwner.left + (rc.right / 2), rcOwner.top + (rc.bottom / 2), 0, 0, SWP_NOSIZE);
return (INT_PTR)TRUE;
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_BUTTONFOPEN:
if (ModifiedFlag == TRUE)
{
if(MessageBox(hDlg, L"Current work not yet saved !\n\nSelect OK button to discard unsaved work\and open new file",
L"Warning", MB_OKCANCEL) == IDCANCEL)
break;
}
ModifiedFlag = FALSE;
if (OpenSaveGetFileName(hDlg, L"All files\0*.*\0", L"File Open", 1, sFname, stmp))
{
dwFsize = GetSrcBufSize(sFname);
if (dwFsize <= 0)
{
Static_SetText(GetDlgItem(hDlg, IDC_FNAME), L"");
Static_SetText(GetDlgItem(hDlg, IDC_STATUS), L"File is Empty, Failed to load !!!");
break;
}
dwBufsize = dwFsize;
if (dwFsize % AES_BLOCKSIZE != 0)
{
dwBufsize = dwFsize / AES_BLOCKSIZE;
dwBufsize = (dwBufsize + 1) * AES_BLOCKSIZE;
}
if(DataBuffer != NULL)
realloc(DataBuffer, dwBufsize);
else
DataBuffer = (BYTE*)malloc(dwBufsize);
ZeroMemory(DataBuffer, dwBufsize);
if (!ReadFromFile(sFname, DataBuffer, dwBufsize))
{
Static_SetText(GetDlgItem(hDlg, IDC_FNAME), L"");
Static_SetText(GetDlgItem(hDlg, IDC_STATUS), L"Failed to read File !!!");
break;
}
Static_SetText(GetDlgItem(hDlg, IDC_STATUS), L"File loaded, Ready for Encryption / Decryption.");
Static_SetText(GetDlgItem(hDlg, IDC_FNAME), sFname);
EnableDisableButtons(hDlg, TRUE);
}
break;
case IDC_BUTTONFSAVE:
if (OpenSaveGetFileName(hDlg, L"All files\0*.*\0", L"File Save", 0, sFname, stmp))
{
if (dwBufsize <= 0)
break;
if (!Write2File(sFname, DataBuffer, dwBufsize))
{
Static_SetText(GetDlgItem(hDlg, IDC_FNAME), L"");
Static_SetText(GetDlgItem(hDlg, IDC_STATUS), L"Failed to save File !!!");
break;
}
Static_SetText(GetDlgItem(hDlg, IDC_STATUS), L"File saved !");
Static_SetText(GetDlgItem(hDlg, IDC_FNAME), sFname);
EnableDisableButtons(hDlg, FALSE);
}
ModifiedFlag = FALSE;
break;
case IDC_BUTTONENCRYPT:
if (AEStype != AES_MODEECB)
{
length = (int)Edit_GetTextLength(GetDlgItem(hDlg, IDC_EDIT2));
if (length != 32)
{
Static_SetText(GetDlgItem(hDlg, IDC_STATUS), L"IV is Incomplete !!!");
break;
}
ZeroMemory(IV, sizeof(IV));
Edit_GetText(GetDlgItem(hDlg, IDC_EDIT2), stmp, length + 1);
KeyFromHex(stmp, IV, length);
}
length = Edit_GetTextLength(GetDlgItem(hDlg, IDC_EDIT1));
if (length != KeyType * KeySize)
{
Static_SetText(GetDlgItem(hDlg, IDC_STATUS), L"Key is Incomplete !!!");
break;
}
ZeroMemory(Key, sizeof(Key));
Edit_GetText(GetDlgItem(hDlg, IDC_EDIT1), stmp, length +1);
if (KeyType == AES_KEYHEX)
KeyFromHex(stmp, Key, length);
else
wcstombs_s(NULL, (char*)Key, length + 1, stmp, length + 1);
RunEncryption(length,ENCRYPT, AEStype, IV);
Static_SetText(GetDlgItem(hDlg, IDC_STATUS), L"Encryption Completed !");
EnableDisableButtons(hDlg, FALSE);
Static_SetText(GetDlgItem(hDlg, IDC_FNAME), sFname);
if (AEStype != AES_MODEECB)
{
HexFromNumber(stmp, IV, 16);
Edit_SetText(GetDlgItem(hDlg, IDC_EDIT2), stmp);
}
Button_Enable(GetDlgItem(hDlg, IDC_BUTTONFSAVE), TRUE);
ModifiedFlag = TRUE;
break;
case IDC_BUTTONDECRYPT:
if (AEStype != AES_MODEECB)
{
length = Edit_GetTextLength(GetDlgItem(hDlg, IDC_EDIT2));
if (length != 32)
{
Static_SetText(GetDlgItem(hDlg, IDC_STATUS), L"IV is Incomplete !!!");
break;
}
ZeroMemory(IV, sizeof(IV));
Edit_GetText(GetDlgItem(hDlg, IDC_EDIT2), stmp, length + 1);
KeyFromHex(stmp, IV, length);
}
length = Edit_GetTextLength(GetDlgItem(hDlg, IDC_EDIT1));
if (length != KeyType * KeySize)
{
Static_SetText(GetDlgItem(hDlg, IDC_STATUS), L"Key is Incomplete !!!");
break;
}
ZeroMemory(Key, sizeof(Key));
Edit_GetText(GetDlgItem(hDlg, IDC_EDIT1), stmp, length + 1);
if (KeyType == AES_KEYHEX)
KeyFromHex(stmp, Key, length);
else
wcstombs_s(NULL, (char*)Key, length + 1, stmp, length + 1);
RunDecryption(length, DECRYPT, AEStype, IV);
Static_SetText(GetDlgItem(hDlg, IDC_STATUS), L"Decryption Completed !");
EnableDisableButtons(hDlg, FALSE);
Static_SetText(GetDlgItem(hDlg, IDC_FNAME), sFname);
Button_Enable(GetDlgItem(hDlg, IDC_BUTTONFSAVE), TRUE);
ModifiedFlag = TRUE;
break;
case IDC_BUTTONAUTOKEY: // generate auto Key
SendMessage(hDlg, WM_COMMAND, IDC_RADIOHEX, 0);
GenerateSimpleKey(Key);
HexFromNumber(stmp, Key, KeySize);
Edit_SetText(GetDlgItem(hDlg, IDC_EDIT1), stmp);
break;
case IDC_BUTTONAUTOIV: // generate auto iv
GenerateSimpleInitializationVector(IV);
HexFromNumber(stmp, IV, 16);
Edit_SetText(GetDlgItem(hDlg, IDC_EDIT2), stmp);
break;
case IDC_EDIT1: //Key entry
length = Edit_GetTextLength(GetDlgItem(hDlg, IDC_EDIT1));
if (length > 0)
UpdateKeyCounter(hDlg);
break;
case IDC_EDIT2: //IV entry
length = Edit_GetTextLength(GetDlgItem(hDlg, IDC_EDIT2));
if (length > 0)
{
StringCchPrintf(stmp, 25, L"%d/%d (in HEX)", length, 32);
Static_SetText(GetDlgItem(hDlg, IDC_COUNTER2), stmp);
}
break;
case IDC_RADIOASCII: // ASCII Key
Button_SetCheck(GetDlgItem(hDlg, IDC_RADIOASCII), TRUE);
Button_SetCheck(GetDlgItem(hDlg, IDC_RADIOHEX), FALSE);
Static_SetText(GetDlgItem(hDlg, IDC_EDIT1), L"");
KeyType = AES_KEYASCII;
UpdateKeyCounter(hDlg);
break;
case IDC_RADIOHEX: // Hex Key
Button_SetCheck(GetDlgItem(hDlg, IDC_RADIOASCII), FALSE);
Button_SetCheck(GetDlgItem(hDlg, IDC_RADIOHEX), TRUE);
Static_SetText(GetDlgItem(hDlg, IDC_EDIT1), L"");
KeyType = AES_KEYHEX;
UpdateKeyCounter(hDlg);
break;
case IDC_RADIOECB: // ECB
case IDC_RADIOCBC: // CBC
case IDC_RADIOCTR: // CTR
case IDC_RADIOOFB: // OFB
case IDC_RADIOCFB: // CFB
SetAEStype(hDlg, LOWORD(wParam));
break;
case IDC_RADIO128: // 128 bit
SetKeySize(hDlg, LOWORD(wParam), AES_KEYSIZE128);
break;
case IDC_RADIO192: // 192 bit
SetKeySize(hDlg, LOWORD(wParam), AES_KEYSIZE192);
break;
case IDC_RADIO256: // 256 bit
SetKeySize(hDlg, LOWORD(wParam), AES_KEYSIZE256);
break;
case IDM_ABOUT:
DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hDlg, (DLGPROC)About, 0);
break;
case IDOK:
case IDCANCEL:
{
free(DataBuffer);
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
}
}
break;
case WM_CLOSE:
EndDialog(hDlg, 0);
return FALSE;
}
return (INT_PTR)FALSE;
}
/*-----------------------------------------------------------------------------------------------
MAIN FUNCTION
-----------------------------------------------------------------------------------------------*/
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,_In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
hInst = hInstance;
return DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)MainDialog, 0);
}