-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
529 lines (418 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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#include <iostream>
#include <cstring>
#include <fstream>
#include <sstream>
#include<bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>
#include "structures.h"
using namespace std;
/* Serves as the initial round during encryption
* AddRoundKey is simply an XOR of a 128-bit block with the 128-bit key.
*/
void AddRoundKey(unsigned char * state, unsigned char * roundKey) {
for (int i = 0; i < 16; i++) {
state[i] ^= roundKey[i];
}
}
/* Perform substitution to each of the 16 bytes
* Uses S-box as lookup table
*/
void SubBytes(unsigned char * state) {
for (int i = 0; i < 16; i++) {
state[i] = s[state[i]];
}
}
// Shift left, adds diffusion
void ShiftRows(unsigned char * state) {
unsigned char tmp[16];
/* Column 1 */
tmp[0] = state[0];
tmp[1] = state[5];
tmp[2] = state[10];
tmp[3] = state[15];
/* Column 2 */
tmp[4] = state[4];
tmp[5] = state[9];
tmp[6] = state[14];
tmp[7] = state[3];
/* Column 3 */
tmp[8] = state[8];
tmp[9] = state[13];
tmp[10] = state[2];
tmp[11] = state[7];
/* Column 4 */
tmp[12] = state[12];
tmp[13] = state[1];
tmp[14] = state[6];
tmp[15] = state[11];
for (int i = 0; i < 16; i++) {
state[i] = tmp[i];
}
}
/* MixColumns uses mul2, mul3 look-up tables
* Source of diffusion
*/
void MixColumns(unsigned char * state) {
unsigned char tmp[16];
tmp[0] = (unsigned char) mul2[state[0]] ^ mul3[state[1]] ^ state[2] ^ state[3];
tmp[1] = (unsigned char) state[0] ^ mul2[state[1]] ^ mul3[state[2]] ^ state[3];
tmp[2] = (unsigned char) state[0] ^ state[1] ^ mul2[state[2]] ^ mul3[state[3]];
tmp[3] = (unsigned char) mul3[state[0]] ^ state[1] ^ state[2] ^ mul2[state[3]];
tmp[4] = (unsigned char)mul2[state[4]] ^ mul3[state[5]] ^ state[6] ^ state[7];
tmp[5] = (unsigned char)state[4] ^ mul2[state[5]] ^ mul3[state[6]] ^ state[7];
tmp[6] = (unsigned char)state[4] ^ state[5] ^ mul2[state[6]] ^ mul3[state[7]];
tmp[7] = (unsigned char)mul3[state[4]] ^ state[5] ^ state[6] ^ mul2[state[7]];
tmp[8] = (unsigned char)mul2[state[8]] ^ mul3[state[9]] ^ state[10] ^ state[11];
tmp[9] = (unsigned char)state[8] ^ mul2[state[9]] ^ mul3[state[10]] ^ state[11];
tmp[10] = (unsigned char)state[8] ^ state[9] ^ mul2[state[10]] ^ mul3[state[11]];
tmp[11] = (unsigned char)mul3[state[8]] ^ state[9] ^ state[10] ^ mul2[state[11]];
tmp[12] = (unsigned char)mul2[state[12]] ^ mul3[state[13]] ^ state[14] ^ state[15];
tmp[13] = (unsigned char)state[12] ^ mul2[state[13]] ^ mul3[state[14]] ^ state[15];
tmp[14] = (unsigned char)state[12] ^ state[13] ^ mul2[state[14]] ^ mul3[state[15]];
tmp[15] = (unsigned char)mul3[state[12]] ^ state[13] ^ state[14] ^ mul2[state[15]];
for (int i = 0; i < 16; i++) {
state[i] = tmp[i];
}
}
/* Each round operates on 128 bits at a time
* The number of rounds is defined in AESEncrypt()
*/
void Round(unsigned char * state, unsigned char * key) {
SubBytes(state);
ShiftRows(state);
MixColumns(state);
AddRoundKey(state, key);
}
// Same as Round() except it doesn't mix columns
void FinalRound(unsigned char * state, unsigned char * key) {
SubBytes(state);
ShiftRows(state);
AddRoundKey(state, key);
}
/* The AES encryption function
* Organizes the confusion and diffusion steps into one function
*/
void AESEncrypt(unsigned char * message, unsigned char * expandedKey, unsigned char * encryptedMessage) {
unsigned char state[16]; // Stores the first 16 bytes of original message
for (int i = 0; i < 16; i++) {
state[i] = message[i];
}
int numberOfRounds = 9;
AddRoundKey(state, expandedKey); // Initial round
for (int i = 0; i < numberOfRounds; i++) {
Round(state, expandedKey + (16 * (i+1)));
}
FinalRound(state, expandedKey + 160);
// Copy encrypted state to buffer
for (int i = 0; i < 16; i++) {
encryptedMessage[i] = state[i];
}
}
// *********************************************DESCRYPTION UTILITIES**************************************
void SubRoundKey(unsigned char * state, unsigned char * roundKey) {
for (int i = 0; i < 16; i++) {
state[i] ^= roundKey[i];
}
}
/* InverseMixColumns uses mul9, mul11, mul13, mul14 look-up tables
* Unmixes the columns by reversing the effect of MixColumns in encryption
*/
void InverseMixColumns(unsigned char * state) {
unsigned char tmp[16];
tmp[0] = (unsigned char)mul14[state[0]] ^ mul11[state[1]] ^ mul13[state[2]] ^ mul9[state[3]];
tmp[1] = (unsigned char)mul9[state[0]] ^ mul14[state[1]] ^ mul11[state[2]] ^ mul13[state[3]];
tmp[2] = (unsigned char)mul13[state[0]] ^ mul9[state[1]] ^ mul14[state[2]] ^ mul11[state[3]];
tmp[3] = (unsigned char)mul11[state[0]] ^ mul13[state[1]] ^ mul9[state[2]] ^ mul14[state[3]];
tmp[4] = (unsigned char)mul14[state[4]] ^ mul11[state[5]] ^ mul13[state[6]] ^ mul9[state[7]];
tmp[5] = (unsigned char)mul9[state[4]] ^ mul14[state[5]] ^ mul11[state[6]] ^ mul13[state[7]];
tmp[6] = (unsigned char)mul13[state[4]] ^ mul9[state[5]] ^ mul14[state[6]] ^ mul11[state[7]];
tmp[7] = (unsigned char)mul11[state[4]] ^ mul13[state[5]] ^ mul9[state[6]] ^ mul14[state[7]];
tmp[8] = (unsigned char)mul14[state[8]] ^ mul11[state[9]] ^ mul13[state[10]] ^ mul9[state[11]];
tmp[9] = (unsigned char)mul9[state[8]] ^ mul14[state[9]] ^ mul11[state[10]] ^ mul13[state[11]];
tmp[10] = (unsigned char)mul13[state[8]] ^ mul9[state[9]] ^ mul14[state[10]] ^ mul11[state[11]];
tmp[11] = (unsigned char)mul11[state[8]] ^ mul13[state[9]] ^ mul9[state[10]] ^ mul14[state[11]];
tmp[12] = (unsigned char)mul14[state[12]] ^ mul11[state[13]] ^ mul13[state[14]] ^ mul9[state[15]];
tmp[13] = (unsigned char)mul9[state[12]] ^ mul14[state[13]] ^ mul11[state[14]] ^ mul13[state[15]];
tmp[14] = (unsigned char)mul13[state[12]] ^ mul9[state[13]] ^ mul14[state[14]] ^ mul11[state[15]];
tmp[15] = (unsigned char)mul11[state[12]] ^ mul13[state[13]] ^ mul9[state[14]] ^ mul14[state[15]];
for (int i = 0; i < 16; i++) {
state[i] = tmp[i];
}
}
// Shifts rows right (rather than left) for decryption
void RetrieveRows(unsigned char * state) {
unsigned char tmp[16];
/* Column 1 */
tmp[0] = state[0];
tmp[1] = state[13];
tmp[2] = state[10];
tmp[3] = state[7];
/* Column 2 */
tmp[4] = state[4];
tmp[5] = state[1];
tmp[6] = state[14];
tmp[7] = state[11];
/* Column 3 */
tmp[8] = state[8];
tmp[9] = state[5];
tmp[10] = state[2];
tmp[11] = state[15];
/* Column 4 */
tmp[12] = state[12];
tmp[13] = state[9];
tmp[14] = state[6];
tmp[15] = state[3];
for (int i = 0; i < 16; i++) {
state[i] = tmp[i];
}
}
/* Perform substitution to each of the 16 bytes
* Uses inverse S-box as lookup table
*/
void SubBytesInverse(unsigned char * state) {
for (int i = 0; i < 16; i++) { // Perform substitution to each of the 16 bytes
state[i] = inv_s[state[i]];
}
}
/* Each round operates on 128 bits at a time
* The number of rounds is defined in AESDecrypt()
* Not surprisingly, the steps are the encryption steps but reversed
*/
void RoundInverse(unsigned char * state, unsigned char * key) {
SubRoundKey(state, key);
InverseMixColumns(state);
RetrieveRows(state);
SubBytesInverse(state);
}
// Same as Round() but no InverseMixColumns
void InitialRoundInverse(unsigned char * state, unsigned char * key) {
SubRoundKey(state, key);
RetrieveRows(state);
SubBytesInverse(state);
}
/* The AES decryption function
* Organizes all the decryption steps into one function
*/
void AESDecrypt(unsigned char * encryptedMessage, unsigned char * expandedKey, unsigned char * decryptedMessage)
{
unsigned char state[16]; // Stores the first 16 bytes of encrypted message
for (int i = 0; i < 16; i++) {
state[i] = encryptedMessage[i];
}
InitialRoundInverse(state, expandedKey+160);
int numberOfRounds = 9;
for (int i = 8; i >= 0; i--) {
RoundInverse(state, expandedKey + (16 * (i + 1)));
}
SubRoundKey(state, expandedKey); // Final round
// Copy decrypted state to buffer
for (int i = 0; i < 16; i++) {
decryptedMessage[i] = state[i];
}
}
int encryptioncall()
{
cout << "=============================" << endl;
cout << " 128-bit AES Encryption Tool " << endl;
cout << "=============================" << endl;
char message[1024];
cout << "Enter the message to encrypt: ";
cin.ignore();
cin.getline(message, sizeof(message));
cout << message << endl;
// Pad message to 16 bytes
int originalLen = strlen((const char *)message);
int paddedMessageLen = originalLen;
if ((paddedMessageLen % 16) != 0) {
paddedMessageLen = (paddedMessageLen / 16 + 1) * 16;
}
unsigned char * paddedMessage = new unsigned char[paddedMessageLen];
for (int i = 0; i < paddedMessageLen; i++) {
if (i >= originalLen) {
paddedMessage[i] = 0;
}
else {
paddedMessage[i] = message[i];
}
}
unsigned char * encryptedMessage = new unsigned char[paddedMessageLen];
string str;
ifstream infile;
infile.open("keyfile", ios::in | ios::binary);
if (infile.is_open())
{
getline(infile, str); // The first line of file should be the key
infile.close();
}
else cout << "Unable to open file";
istringstream hex_chars_stream(str);
unsigned char key[16];
int i = 0;
unsigned int c;
while (hex_chars_stream >> hex >> c)
{
key[i] = c;
i++;
}
unsigned char expandedKey[176];
KeyExpansion(key, expandedKey);
for (int i = 0; i < paddedMessageLen; i += 16) {
AESEncrypt(paddedMessage+i, expandedKey, encryptedMessage+i);
}
cout << "Encrypted message in hex:" << endl;
for (int i = 0; i < paddedMessageLen; i++) {
cout << hex << (int) encryptedMessage[i];
cout << " ";
}
cout << endl;
cout<<"Enter name of the encrypted file: ";
string n;
cin>>n;
// Write the encrypted string out to file "message.aes"
ofstream outfile;
outfile.open(n, ios::out | ios::binary);
if (outfile.is_open())
{
outfile << encryptedMessage;
outfile.close();
cout << "Wrote encrypted message to file ";
cout<<n<<endl;
}
else cout << "Unable to open file";
// Free memory
delete[] paddedMessage;
delete[] encryptedMessage;
return 0;
}
int decryptioncall()
{
cout << "=============================" << endl;
cout << " 128-bit AES Decryption Tool " << endl;
cout << "=============================" << endl;
// Read in the message from message.aes
string msgstr;
ifstream infile;
string na;
cout<<"Enter file name to decrypt: ";
cin>>na;
infile.open(na, ios::in | ios::binary);
if (infile.is_open())
{
getline(infile, msgstr); // The first line of file is the message
cout << "Read in encrypted message from message.aes" << endl;
infile.close();
}
else cout << "Unable to open file";
char * msg = new char[msgstr.size()+1];
strcpy(msg, msgstr.c_str());
int n = strlen((const char*)msg);
unsigned char * encryptedMessage = new unsigned char[n];
for (int i = 0; i < n; i++) {
encryptedMessage[i] = (unsigned char)msg[i];
}
// Free memory
delete[] msg;
// Read in the key
string keystr;
ifstream keyfile;
keyfile.open("keyfile", ios::in | ios::binary);
if (keyfile.is_open())
{
getline(keyfile, keystr); // The first line of file should be the key
cout << "Read in the 128-bit key from keyfile" << endl;
keyfile.close();
}
else cout << "Unable to open file";
istringstream hex_chars_stream(keystr);
unsigned char key[16];
int i = 0;
unsigned int c;
while (hex_chars_stream >> hex >> c)
{
key[i] = c;
i++;
}
unsigned char expandedKey[176];
KeyExpansion(key, expandedKey);
int messageLen = strlen((const char *)encryptedMessage);
unsigned char * decryptedMessage = new unsigned char[messageLen];
for (int i = 0; i < messageLen; i += 16) {
AESDecrypt(encryptedMessage + i, expandedKey, decryptedMessage + i);
}
cout << "Decrypted message in hex:" << endl;
for (int i = 0; i < messageLen; i++) {
cout << hex << (int)decryptedMessage[i];
cout << " ";
}
cout << endl;
cout << "Decrypted message: ";
for (int i = 0; i < messageLen; i++) {
cout << decryptedMessage[i];
}
cout << endl;
ofstream outfile;
outfile.open("DecryptedMessage.txt", ios::out | ios::binary);
if (outfile.is_open())
{
outfile << decryptedMessage;
outfile.close();
cout << "Wrote decrypted message to file DecryptedMessage.txt" << endl;
}
delete[] decryptedMessage;
return 0;
}
int main()
{
vector<string> username;
vector<string> password;
username.push_back("aviral");
username.push_back("meghna");
username.push_back("aman");
username.push_back("keshav");
password.push_back("aviral123");
password.push_back("meghna123");
password.push_back("aman123");
password.push_back("keshav123");
string user;
string pass;
cout<<"$$$$$ LOGIN INTO YOUR ACCOUNT $$$$$"<<endl;
cout<<"Username: ";
cin>>user;
cout<<endl;
cout<<"Password: ";
cin>>pass;
int found=0;
for(int i=0;i<username.size();i++)
{
if(username[i]==user && password[i]==pass)
{
found=1;
break;
}
}
cin.ignore();
if(found==1)
{
while(1)
{
cout<<"=============================="<<endl;
cout<<"Press E: To encrypt your data"<<endl;
cout<<"Press D: To decrypt your data"<<endl;
cout<<"Press n: To exit"<<endl;
cout<<"=============================="<<endl;
char choice;
cin>>choice;
switch (choice)
{
case 'E': encryptioncall();break;
case 'e': encryptioncall();break;
case 'D': decryptioncall();break;
case 'd': decryptioncall();break;
case 'n': exit(0);
}}
}
else
cout<<"PLEASE VALID CREDENTIAL";
return 0;
}