-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes.cpp
329 lines (289 loc) · 10.8 KB
/
aes.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
#include <cryptopp/aes.h>
#include <cryptopp/osrng.h>
#include <cryptopp/modes.h>
#include <cryptopp/filters.h>
#include <cryptopp/hex.h>
#include <cryptopp/files.h>
#include <cryptopp/xtrcrypt.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace CryptoPP;
using namespace std;
int mode, randomOption = 1, encdecOption, inputOption;
string filename, plain, cipher, cipher_hex, recovered, key_hex, iv_hex, key, iv;
int main(int argc, char* argv[]) {
cout << "======== Buoc 1: Chon mode =========" << endl;
cout << "1. ECB" << endl;
cout << "2. CBC" << endl;
cout << "3. OFB" << endl;
cout << "4. CFB" << endl;
cout << "5. CTR" << endl;
cout << "6. XTS" << endl;
cout << "7. CCM" << endl;
cout << "Nhap stt 1-7 tu de chon mode: ";
cin >> mode;
while (!(mode >= 1 && mode <= 7)) {
cout << "STT mode khong hop le, vui long nhap lai: ";
cin >> mode;
}
cout << "======== Buoc 2: Nhap input, key va IV ========" << endl;
cout << "Nhap 0 de ma hoa, 1 de giai ma: ";
cin >> encdecOption;
cout << "Nhap 0 de doc tu man hinh, 1 de doc tu file: ";
cin >> inputOption;
if (encdecOption == 0) {
cout << "Nhap 0 de sinh key va iv ngau nhien, 1 neu khong muon: ";
cin >> randomOption;
}
if (inputOption == 0) {
if (encdecOption == 0) {
cout << "Nhap plaintext: ";
cin.ignore();
getline(cin, plain);
} else {
cout << "Nhap cipher (hexadecimal): ";
cin >> cipher_hex;
// Convert cipher from hex to binary
CryptoPP::StringSource(cipher_hex, true, new CryptoPP::HexDecoder(new CryptoPP::StringSink(cipher)));
}
if (randomOption == 1) {
cout << "Nhap key (dang hexadecimal): ";
cin >> key_hex;
cout << "Nhap IV (neu co) (dang hexadecimal): ";
cin >> iv_hex;
}
} else {
cout << "Nhap file can doc: ";
cin >> filename;
ifstream file(filename);
if (encdecOption == 0) {
getline(file, plain);
} else {
getline(file, cipher_hex);
// Convert cipher from hex to binary
CryptoPP::StringSource(cipher_hex, true, new CryptoPP::HexDecoder(new CryptoPP::StringSink(cipher)));
}
if (randomOption == 1) {
getline(file, key_hex);
getline(file, iv_hex);
}
file.close();
}
// Convert key and iv from hex to binary
CryptoPP::StringSource(key_hex, true, new CryptoPP::HexDecoder(new CryptoPP::StringSink(key)));
CryptoPP::StringSource(iv_hex, true, new CryptoPP::HexDecoder(new CryptoPP::StringSink(iv)));
// Generate key and iv
CryptoPP::byte bkey[AES::DEFAULT_KEYLENGTH], biv[AES::BLOCKSIZE];
AutoSeededRandomPool prng;
if (randomOption == 0) {
// Generate a random key and IV
prng.GenerateBlock(bkey, sizeof(bkey));
prng.GenerateBlock(biv, sizeof(biv));
}
cout << "\n=========== Buoc 3: Encrypt hoac decrypt ==========\n";
// Print key and iv if we generate those randomly
if (randomOption == 0) {
// Convert key, iv from byte bin to hex
CryptoPP::StringSource(bkey, sizeof(bkey), true, new CryptoPP::HexEncoder(new CryptoPP::StringSink(key_hex)));
CryptoPP::StringSource(biv, sizeof(bkey), true, new CryptoPP::HexEncoder(new CryptoPP::StringSink(iv_hex)));
// Convert key, iv from hex to bin
CryptoPP::StringSource(key_hex, true, new CryptoPP::HexDecoder(new CryptoPP::StringSink(key)));
CryptoPP::StringSource(iv_hex, true, new CryptoPP::HexDecoder(new CryptoPP::StringSink(iv)));
cout << "Key: " << key_hex << endl;
if (mode != 1) cout << "IV: " << iv_hex << endl;
}
//
if (mode == 1) {
// MODE ECB
if (!encdecOption) {
// Create the AES encryption object
CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption aes;
aes.SetKey((const CryptoPP::byte*)key.data(), key.size());
// Encrypt the plaintext using AES-ECB
CryptoPP::StringSource(plain, true,
new CryptoPP::StreamTransformationFilter(aes,
new CryptoPP::StringSink(cipher)
)
);
// Print the ciphertext in hexadecimal format
std::cout << "Ciphertext: ";
CryptoPP::StringSource ss1(cipher, true,
new CryptoPP::HexEncoder(
new CryptoPP::FileSink(std::cout)
)
);
} else {
// Create the AES decryption object
CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption aes;
aes.SetKey((const CryptoPP::byte*)key.data(), key.size());
// Decrypt ciphertext and print out
CryptoPP::StringSource(cipher, true, new CryptoPP::StreamTransformationFilter(aes, new CryptoPP::StringSink(plain)));
cout << "Plain text: " << plain;
}
} else if (mode == 2) {
// MODE CBC
if (!encdecOption) {
// Create the AES encryption object
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption aes;
aes.SetKeyWithIV((const CryptoPP::byte*)key.data(), key.size(), (const CryptoPP::byte*)iv.data());
// Encrypt the plaintext using AES-CBC
CryptoPP::StringSource(plain, true,
new CryptoPP::StreamTransformationFilter(aes,
new CryptoPP::StringSink(cipher)
)
);
// Print the ciphertext in hexadecimal format
std::cout << "Ciphertext: ";
CryptoPP::StringSource ss1(cipher, true,
new CryptoPP::HexEncoder(
new CryptoPP::FileSink(std::cout)
)
);
} else {
// Create the AES decryption object
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption aes;
aes.SetKeyWithIV((const CryptoPP::byte*)key.data(), key.size(), (const CryptoPP::byte*)iv.data());
// Decrypt ciphertext and print out
CryptoPP::StringSource(cipher, true, new CryptoPP::StreamTransformationFilter(aes, new CryptoPP::StringSink(plain)));
cout << "Plain text: " << plain;
}
} else if (mode == 3) {
// MODE OFB
if (!encdecOption) {
// Create the AES encryption object
CryptoPP::OFB_Mode<CryptoPP::AES>::Encryption aes;
aes.SetKeyWithIV((const CryptoPP::byte*)key.data(), key.size(), (const CryptoPP::byte*)iv.data());
// Encrypt the plaintext using AES-OFB
CryptoPP::StringSource(plain, true,
new CryptoPP::StreamTransformationFilter(aes,
new CryptoPP::StringSink(cipher)
)
);
// Print the ciphertext in hexadecimal format
std::cout << "Ciphertext: ";
CryptoPP::StringSource ss1(cipher, true,
new CryptoPP::HexEncoder(
new CryptoPP::FileSink(std::cout)
)
);
} else {
// Create the AES decryption object
CryptoPP::OFB_Mode<CryptoPP::AES>::Decryption aes;
aes.SetKeyWithIV((const CryptoPP::byte*)key.data(), key.size(), (const CryptoPP::byte*)iv.data());
// Decrypt ciphertext and print out
CryptoPP::StringSource(cipher, true, new CryptoPP::StreamTransformationFilter(aes, new CryptoPP::StringSink(plain)));
cout << "Plain text: " << plain;
}
} else if (mode == 4) {
// MODE CFB
if (!encdecOption) {
// Create the AES encryption object
CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption aes;
aes.SetKeyWithIV((const CryptoPP::byte*)key.data(), key.size(), (const CryptoPP::byte*)iv.data());
// Encrypt the plaintext using AES-CFB
CryptoPP::StringSource(plain, true,
new CryptoPP::StreamTransformationFilter(aes,
new CryptoPP::StringSink(cipher)
)
);
// Print the ciphertext in hexadecimal format
std::cout << "Ciphertext: ";
CryptoPP::StringSource ss1(cipher, true,
new CryptoPP::HexEncoder(
new CryptoPP::FileSink(std::cout)
)
);
} else {
// Create the AES decryption object
CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption aes;
aes.SetKeyWithIV((const CryptoPP::byte*)key.data(), key.size(), (const CryptoPP::byte*)iv.data());
// Decrypt ciphertext and print out
CryptoPP::StringSource(cipher, true, new CryptoPP::StreamTransformationFilter(aes, new CryptoPP::StringSink(plain)));
cout << "Plain text: " << plain;
}
} else if (mode == 5) {
// MODE CTR
if (!encdecOption) {
// Create the AES encryption object
CryptoPP::CTR_Mode<CryptoPP::AES>::Encryption aes;
aes.SetKeyWithIV((const CryptoPP::byte*)key.data(), key.size(), (const CryptoPP::byte*)iv.data());
// Encrypt the plaintext using AES-CTR
CryptoPP::StringSource(plain, true,
new CryptoPP::StreamTransformationFilter(aes,
new CryptoPP::StringSink(cipher)
)
);
// Print the ciphertext in hexadecimal format
std::cout << "Ciphertext: ";
CryptoPP::StringSource ss1(cipher, true,
new CryptoPP::HexEncoder(
new CryptoPP::FileSink(std::cout)
)
);
} else {
// Create the AES decryption object
CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption aes;
aes.SetKeyWithIV((const CryptoPP::byte*)key.data(), key.size(), (const CryptoPP::byte*)iv.data());
// Decrypt ciphertext and print out
CryptoPP::StringSource(cipher, true, new CryptoPP::StreamTransformationFilter(aes, new CryptoPP::StringSink(plain)));
cout << "Plain text: " << plain;
}
} else if (mode == 6) {
// MODE XTS
if (!encdecOption) {
// Create the AES encryption object
CryptoPP::CTR_Mode<CryptoPP::AES>::Encryption aes;
aes.SetKeyWithIV((const CryptoPP::byte*)key.data(), key.size(), (const CryptoPP::byte*)iv.data());
// Encrypt the plaintext using AES-XTS
CryptoPP::StringSource(plain, true,
new CryptoPP::StreamTransformationFilter(aes,
new CryptoPP::StringSink(cipher)
)
);
// Print the ciphertext in hexadecimal format
std::cout << "Ciphertext: ";
CryptoPP::StringSource ss1(cipher, true,
new CryptoPP::HexEncoder(
new CryptoPP::FileSink(std::cout)
)
);
} else {
// Create the AES decryption object
CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption aes;
aes.SetKeyWithIV((const CryptoPP::byte*)key.data(), key.size(), (const CryptoPP::byte*)iv.data());
// Decrypt ciphertext and print out
CryptoPP::StringSource(cipher, true, new CryptoPP::StreamTransformationFilter(aes, new CryptoPP::StringSink(plain)));
cout << "Plain text: " << plain;
}
} else if (mode == 7) {
// MODE CCM
if (!encdecOption) {
// Create the AES encryption object
CryptoPP::CTR_Mode<CryptoPP::AES>::Encryption aes;
aes.SetKeyWithIV((const CryptoPP::byte*)key.data(), key.size(), (const CryptoPP::byte*)iv.data());
// Encrypt the plaintext using AES-CCM
CryptoPP::StringSource(plain, true,
new CryptoPP::StreamTransformationFilter(aes,
new CryptoPP::StringSink(cipher)
)
);
// Print the ciphertext in hexadecimal format
std::cout << "Ciphertext: ";
CryptoPP::StringSource ss1(cipher, true,
new CryptoPP::HexEncoder(
new CryptoPP::FileSink(std::cout)
)
);
} else {
// Create the AES decryption object
CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption aes;
aes.SetKeyWithIV((const CryptoPP::byte*)key.data(), key.size(), (const CryptoPP::byte*)iv.data());
// Decrypt ciphertext and print out
CryptoPP::StringSource(cipher, true, new CryptoPP::StreamTransformationFilter(aes, new CryptoPP::StringSink(plain)));
cout << "Plain text: " << plain;
}
}
return 0;
}