-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAES_DH.cpp
More file actions
514 lines (454 loc) · 17.9 KB
/
Copy pathAES_DH.cpp
File metadata and controls
514 lines (454 loc) · 17.9 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
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
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
#include <conio.h>
#include <string.h>
#include <time.h>
#include <math.h>
using namespace std;
unsigned char s_box[256] = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
};
unsigned char mul2[] = {
0x00,0x02,0x04,0x06,0x08,0x0a,0x0c,0x0e,0x10,0x12,0x14,0x16,0x18,0x1a,0x1c,0x1e,
0x20,0x22,0x24,0x26,0x28,0x2a,0x2c,0x2e,0x30,0x32,0x34,0x36,0x38,0x3a,0x3c,0x3e,
0x40,0x42,0x44,0x46,0x48,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,0x5e,
0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6e,0x70,0x72,0x74,0x76,0x78,0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d,0x0e,0x07,0x04,0x01,0x02,0x13,0x10,0x15,0x16,0x1f,0x1c,0x19,0x1a
};
unsigned char rcon[256] = {
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39,
0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8,
0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef,
0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc,
0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b,
0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3,
0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94,
0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35,
0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f,
0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04,
0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63,
0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d
};
void KeyExpansionCore(unsigned char* in, unsigned char i){
/*unsigned int *q=(unsigned int*)in;
*q=(*q>>8)| ((*q &0xff)<<24);*/
unsigned char t=in[0];
in[0]=in[1];
in[1]=in[2];
in[2]=in[3];
in[3]=t;
in[0]=s_box[in[0]]; in[1]=s_box[in[1]];
in[2]=s_box[in[2]]; in[3]=s_box[in[3]];
in[0]^=rcon[i];
}
void KeyExpansion(unsigned char *inputKey, unsigned char *expandedKeys){
for(int i=0; i<16;i++)
{
expandedKeys[i]=inputKey[i];
}
int bytesGenerated=16;
int rconIteration=1;
unsigned char temp[4];
while (bytesGenerated<176)
{
for(int i=0;i<4;i++)
temp[i]=expandedKeys[i+bytesGenerated-4];
if(bytesGenerated % 16==0)
KeyExpansionCore(temp,rconIteration++);
for(unsigned char a=0;a<4;a++)
{
expandedKeys[bytesGenerated]=expandedKeys[bytesGenerated-16]^temp[a];
bytesGenerated++;
}
}
}
void SubBytes(unsigned char* state){
for(int i=0;i<16;i++){
state[i]=s_box[state[i]];
}
}
void ShiftRows(unsigned char* state){
unsigned char tmp[16];
tmp[0]=state[0];
tmp[1]=state[5];
tmp[2]=state[10];
tmp[3]=state[15];
tmp[4]=state[4];
tmp[5]=state[9];
tmp[6]=state[14];
tmp[7]=state[3];
tmp[8]=state[8];
tmp[9]=state[13];
tmp[10]=state[2];
tmp[11]=state[7];
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];
}
}
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];
}
void AddRoundKey(unsigned char* state,unsigned char* roundKey){
for(int i=0;i<16;i++){
state[i]^=roundKey[i];
}
}
void AES_Encrypt(unsigned char* message,unsigned char* key){
unsigned char state[16];
for(int i=0;i<16;i++){
state[i]=message[i];
}
int numberOfRounds=9;
unsigned char expandedKey[176];
KeyExpansion(key,expandedKey);
AddRoundKey(state,key);
for(int i=0;i<numberOfRounds;i++){
SubBytes(state);
ShiftRows(state);
MixColumns(state);
AddRoundKey(state,expandedKey+(16*(i+1)));
}
//Final Round
SubBytes(state);
ShiftRows(state);
AddRoundKey(state,expandedKey+160);
for(int i=0;i<16;i++){
message[i]=state[i];
}
}
void PrintHex(unsigned char x){
if(x/16<10) cout<<(char)((x/16)+'0');
if(x/16>=10) cout<<(char)((x/16-10)+'A');
if(x%16<10) cout<<(char)((x%16)+'0');
if(x%16>=10) cout<<(char)((x%16-10)+'A');
}
// Function to compute `a^m mod n`
int compute(int a, int m, int n)
{
int r;
int y = 1;
while (m > 0)
{
r = m % 2;
// fast exponention
if (r == 1) {
y = (y*a) % n;
}
a = a*a % n;
m = m / 2;
}
return y;
}
int generate(){
srand(time(NULL));
int primeN=rand(), flag=0,i;
do{
primeN=(rand()%11)+1;
for(i=2;i<primeN/2;i++)
{
if(primeN%i==0)
break;
}
if(i==primeN/2)
flag=1;
}while(flag!=1);
//cout<<primeN<<endl;
return primeN;
}
int gcd(int a, int b)
{
int q, r;
while (b > 0)
{
q = a / b;
r = a - q * b;
a = b;
b = r;
}
return a;
}
int gens(int a)
{
char ch, op;
int k;
int mode = 0;
int val;
int ord[50], g[50], H[50];
bool cyclic = false;
int elements[100];
int order;
ch='*';
mode = 1;
op='*';
if (mode)
{
//printf("G=<Z%d*,%c>\n", a, op);
k = 0;
for (int i = 0; i < a; i++)
{
if (gcd(a, i) == 1) { // co-prime
elements[k++] = i;
}
}
elements[k] = '\0';
mode = 0;
}
else {
//printf("G=<Z%d,%c>\n", a, op);
op='*';
for (int i = 0; i < a; i++) {
elements[i] = i;
}
k = a;
}
/*printf("G = {");
for (int i = 0; i < k; i++)
{
printf("%d", elements[i]);
(i < k-1) ? printf(",") : printf("}");
}*/
order = k;
//printf("\nThe order of the group is %d.\n\n", order);
for (int i = 0; i < k; i++)
{
int temp = 1;
while (1)
{
if (op == '*')
{
val = (int)(pow(elements[i], temp));
if (val % a == 1)
{
ord[i] = temp;
//printf("order(%d) = %d\n", elements[i], temp);
break;
}
}
temp++;
}
}
int x = 0, count = 0;
for (int i = 0; i < k; i++)
{
if (ord[i] == order)
{
g[x++] = elements[i];
cyclic = true;
count++;
}
}
if (cyclic)
{
//printf("A group is a cyclic group with %d generators", count);
srand(time(NULL));
return g[rand()%count];
}
else {
cout<<"The group is not a cyclic group."<<endl;
exit(0);
}
printf("\n\n——————————————————————————————————————————————\n\n");
}
int main()
{
srand(time(NULL));
int p = generate(); // modulus
printf("\np: %d",p);
int g = gens(p); // base
int a, b; // `a` – Alice's secret key, `b` – Bob's secret key.
int A, B; // `A` – Alice's public key, `B` – Bob's public key
// choose a secret integer for Alice's private key (only known to Alice)
a = rand() % p; // or, use `rand()`
printf("\na: %d",a);
// Calculate Alice's public key (Alice will send `A` to Bob)
A = compute(g, a, p);
// choose a secret integer for Bob's private key (only known to Bob)
b = rand() % p; // or, use `rand()`
printf("\nb: %d\n",b);
// Calculate Bob's public key (Bob will send `B` to Alice)
B = compute(g, b, p);
// Alice and Bob Exchange their public key `A` and `B` with each other
// Find secret key
int keyA = compute(B, a, p);
int keyB = compute(A, b, p);
unsigned char key[16];
cout<<"Alice's secret key is "<<keyA<<endl;
cout<<" Bob's secret key is "<<keyB<<endl;
int i;
unsigned char a1[]={12,14,67,4,34,65,37,23,17,98,45,35,56,10,21,19};
unsigned char a2[]={1,4,6,4,38,64,33,22,11,99,55,39,65,15,23,18};
unsigned char a3[]={24,28,63,8,68,30,44,46,34,95,90,70,56,20,42,38};
unsigned char a4[]={4,23,69,81,50,33,41,66,35,57,9,7,6,29,27,3};
unsigned char a5[]={87,82,76,4,43,56,73,32,71,89,54,53,65,1,12,91};
unsigned char a6[]={25,14,62,40,49,52,78,31,73,81,51,36,26,10,22,78};
unsigned char a7[]={64,94,52,8,86,21,46,6,42,9,4,30,51,15,22,13};
unsigned char a8[]={29,47,67,40,36,65,34,23,15,98,45,35,12,30,44,26};
unsigned char a9[]={28,14,69,4,32,66,37,23,7,98,5,35,56,19,27,10};
unsigned char a10[]={12,15,16,13,14,11,17,10,7,9,6,5,4,3,2,1};
unsigned char a11[]={1,14,7,4,3,6,14,46,38,9,5,37,57,17,27,19};
switch(keyA){
case 1:
for(i=0;i<16;i++)
key[i]=a1[i];
break;
case 2:
for(i=0;i<16;i++)
key[i]=a2[i];
break;
case 3:
for(i=0;i<16;i++)
key[i]=a3[i];
break;
case 4:
for(i=0;i<16;i++)
key[i]=a4[i];
break;
case 5:
for(i=0;i<16;i++)
key[i]=a5[i];
break;
case 6:
for(i=0;i<16;i++)
key[i]=a6[i];
break;
case 7:
for(i=0;i<16;i++)
key[i]=a7[i];
break;
case 8:
for(i=0;i<16;i++)
key[i]=a8[i];
break;
case 9:
for(i=0;i<16;i++)
key[i]=a9[i];
break;
case 10:
for(i=0;i<16;i++)
key[i]=a10[i];
break;
case 11:
for(i=0;i<16;i++)
key[i]=a11[i];
break;
}
cout<<"The shared key is: ";
for(i=0;i<16;i++){
printf("%d ", key[i]);
}
printf("\n");
//unsigned char message[]="This is a message we will encrypt with AES!";
unsigned char message[100];
i=0;
printf("Enter the message to be encrypted: ");
while((message[i]=getchar())!='\n'){
i++;
}
message[i]='\0';
//scanf("%s",message);
//unsigned char message[]="This is AES Encryption";
cout<<message;
//unsigned char key[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
//unsigned char key[16]={11,22,33,44,55,66,77,88,99,10,31,27,34,43,59,62};
int originalLen= strlen((const char*)message);
int lenOfPaddedMessage=originalLen;
if(lenOfPaddedMessage%16!=0){
lenOfPaddedMessage=(lenOfPaddedMessage/16 + 1)*16;
}
unsigned char* paddedMessage = new unsigned char[lenOfPaddedMessage];
for(int i=0;i<lenOfPaddedMessage;i++){
if(i>=originalLen){
paddedMessage[i]=0;
}
else{
paddedMessage[i]=message[i];
}
}
for(int i=0;i<lenOfPaddedMessage;i+=16){
AES_Encrypt(paddedMessage+i,key);
}
cout<<"\nEncrypted message: "<<endl;
for(int i=0;i<lenOfPaddedMessage;i++){
PrintHex(paddedMessage[i]);
cout<<" ";
}
getch();
delete[] paddedMessage;
return 0;
}