-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAES.cpp
More file actions
101 lines (75 loc) · 2.72 KB
/
Copy pathAES.cpp
File metadata and controls
101 lines (75 loc) · 2.72 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
#include <iostream>
#include <vector>
using namespace std;
// The AES key schedule.
vector<unsigned char> key_schedule(const vector<unsigned char>& key) {
vector<unsigned char> expanded_key(16 * 14);
for (int i = 0; i < 16; i++) {
expanded_key[i] = key[i];
}
for (int r = 1; r < 14; r++) {
for (int i = 0; i < 4; i++) {
expanded_key[16 * r + i] = expanded_key[16 * (r - 1) + i] ^ sub_bytes(expanded_key[16 * (r - 1) + i + 4]);
}
for (int i = 0; i < 4; i++) {
expanded_key[16 * r + i + 4] = expanded_key[16 * r + i] ^ shift_rows(expanded_key[16 * r + i]);
}
expanded_key[16 * r + 12] = expanded_key[16 * r + 12] ^ rcon[r - 1];
}
return expanded_key;
}
// The AES encryption function.
vector<unsigned char> encrypt(const vector<unsigned char>& plaintext, const vector<unsigned char>& key) {
vector<unsigned char> expanded_key = key_schedule(key);
for (int round = 0; round < 10; round++) {
plaintext = add_round_key(plaintext, expanded_key, round);
plaintext = sub_bytes(plaintext);
plaintext = shift_rows(plaintext);
plaintext = mix_columns(plaintext);
}
plaintext = add_round_key(plaintext, expanded_key, 10);
return plaintext;
}
// The AES decryption function.
vector<unsigned char> decrypt(const vector<unsigned char>& ciphertext, const vector<unsigned char>& key) {
vector<unsigned char> expanded_key = key_schedule(key);
for (int round = 10; round > 0; round--) {
ciphertext = add_round_key(ciphertext, expanded_key, round);
ciphertext = inv_shift_rows(ciphertext);
ciphertext = inv_sub_bytes(ciphertext);
ciphertext = add_round_key(ciphertext, expanded_key, round - 1);
}
ciphertext = inv_shift_rows(ciphertext);
ciphertext = inv_sub_bytes(ciphertext);
ciphertext = add_round_key(ciphertext, expanded_key, 0);
return ciphertext;
}
int main() {
// Get the key from the user.
string key;
cout << "Enter the key: ";
getline(cin, key);
// Convert the key to a vector of unsigned char.
vector<unsigned char> key_vector(key.begin(), key.end());
// Get the plaintext from the user.
string plaintext;
cout << "Enter the plaintext: ";
getline(cin, plaintext);
// Convert the plaintext to a vector of unsigned char.
vector<unsigned char> plaintext_vector(plaintext.begin(), plaintext.end());
// Encrypt the plaintext.
vector<unsigned char> ciphertext = encrypt(plaintext_vector, key_vector);
// Print the ciphertext.
for (unsigned char c : ciphertext) {
cout << c;
}
cout << endl;
// Decrypt the ciphertext.
vector<unsigned char> decrypted_plaintext = decrypt(ciphertext, key_vector);
// Print the decrypted plaintext.
for (unsigned char c : decrypted_plaintext) {
cout << c;
}
cout << endl;
return 0;
}