-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
122 lines (92 loc) · 3.23 KB
/
Copy pathmain.cpp
File metadata and controls
122 lines (92 loc) · 3.23 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
// imports needed libraries and AES header
#include "AES.h"
#include <iostream>
#include <string>
using namespace std;
int main(){
AES vals; // initialize AES struct
// plaintext
unsigned char pt[] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff};
// array to hold words that make up the 128 bit key
string k1[] = {"00010203", "04050607", "08090a0b", "0c0d0e0f"};
// array to hold words that make up the 192 bit key
string k2[] = {"00010203","04050607","08090a0b","0c0d0e0f","10111213","14151617"};
// array to hold words that make up the 256 bit key
string k3[] = {"00010203","04050607","08090a0b","0c0d0e0f","10111213","14151617","18191a1b","1c1d1e1f"};
// stores plaintext into AES struct
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
vals.state[j][i] = (int)pt[i + (4*j)];
}
}
cout << "C.1 AES-128 (Nk=4, Nr=10)" << endl << endl;
cout << "PLAINTEXT: ";
printState(vals);
// stores needed values into AES struct
vals.Nk = 4;
vals.Nr = 10;
vals.key = k1;
cout << "KEY: ";
printKeys(vals);
cout << endl << "CIPHER (ENCRYPT):" << endl;
// creates pointer to hold cipher key and all round keys
int *w = new int[(1+vals.Nr)*vals.Nk]{};
// runs key expansion
w = keyExpansion(vals, w);
// runs encryption algorithm
vals = Cipher(vals, w);
cout << "INVERSE CIPHER (DECRYPT):" << endl;
vals = invCipher(vals, w); // runs decryption algorithm
cout << endl;
delete w; // frees up memory from pointer
// resets plaintext held in AES struct
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
vals.state[j][i] = (int)pt[i + (4*j)];
}
}
cout << "C.2 AES-192 (Nk=6, Nr=12)" << endl << endl;
cout << "PLAINTEXT: ";
printState(vals); // prints plaintext
// saves needed values to AES struct
vals.Nk = 6;
vals.Nr = 12;
vals.key = k2;
cout << "KEY: ";
printKeys(vals); // prints key
cout << endl << "CIPHER (ENCRYPT):" << endl;
// gets cipher key and round keys
int *w2 = new int[(1+vals.Nr)*vals.Nk]{};
w2 = keyExpansion(vals, w2);
vals = Cipher(vals, w2); // run encryption
cout << "INVERSE CIPHER (DECRYPT):" << endl;
vals = invCipher(vals, w2); // run decryption
cout << endl;
delete w2; // free memory from pointer
// reset plaintext in AES struct
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
vals.state[j][i] = (int)pt[i + (4*j)];
}
}
cout << "C.3 AES-256 (Nk=8, Nr=14)" << endl << endl;
cout << "PLAINTEXT: ";
printState(vals); // prints plaintext
// sets needed values in AES struct
vals.Nk = 8;
vals.Nr = 14;
vals.key = k3;
cout << "KEY: ";
printKeys(vals); // prints key
cout << endl << "CIPHER (ENCRYPT):" << endl;
// gets cipher key and round keys
int *w3 = new int[(1+vals.Nr)*vals.Nk]{};
w3 = keyExpansion(vals, w3);
// runs encryption
vals = Cipher(vals, w3);
// runs decryption
cout << "INVERSE CIPHER (DECRYPT):" << endl;
vals = invCipher(vals, w3);
delete w3; // free memory from pointer
return 0;
}