/
aes-modes.cpp
93 lines (74 loc) · 3.61 KB
/
aes-modes.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
#include <iostream>
#include <iomanip>
#include <cryptopp/modes.h>
#include <cryptopp/aes.h>
#include <cryptopp/filters.h>
/*
* AES Encryption
*
* Introduction to Cryptography Homework #3
*
* Source Code: https://git.io/AES
*
* 2021-04-04 by Sean, 0816146 韋詠祥
*/
using namespace std;
using namespace CryptoPP;
int main() {
const byte key[17] = "1234567890ABCDEF";
const byte iv0[17] = "0000000000000000";
const byte iv9[17] = "9999999999999999";
const string plaintext = "AES is the block cipher standard.";
string cfbCip, cb0Cip, cb9Cip, ecbCip;
string cfbRec, cb0Rec, cb9Rec, ecbRec;
const AlgorithmParameters cfbP = MakeParameters
(Name::FeedbackSize(), 4)
(Name::IV(), ConstByteArrayParameter(iv0, 16));
/* Encryption */
CFB_Mode<AES>::Encryption cfbEnc; cfbEnc.SetKey(key, 16, cfbP);
CBC_Mode<AES>::Encryption cb0Enc(key, 16, iv0);
CBC_Mode<AES>::Encryption cb9Enc(key, 16, iv9);
ECB_Mode<AES>::Encryption ecbEnc(key, 16);
StreamTransformationFilter cfbStfE(cfbEnc, new StringSink(cfbCip), StreamTransformationFilter::NO_PADDING);
StreamTransformationFilter cb0StfE(cb0Enc, new StringSink(cb0Cip), StreamTransformationFilter::ZEROS_PADDING);
StreamTransformationFilter cb9StfE(cb9Enc, new StringSink(cb9Cip), StreamTransformationFilter::PKCS_PADDING);
StreamTransformationFilter ecbStfE(ecbEnc, new StringSink(ecbCip), StreamTransformationFilter::PKCS_PADDING);
cfbStfE.Put(reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length());
cb0StfE.Put(reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length());
cb9StfE.Put(reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length());
ecbStfE.Put(reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length());
cfbStfE.MessageEnd();
cb0StfE.MessageEnd();
cb9StfE.MessageEnd();
ecbStfE.MessageEnd();
/* Write answer to file */
FILE *fd = fopen("out.txt", "w");
for (const byte c : cfbCip) { fprintf(fd, "%02x ", c & 0xFF); } fputs("\n", fd);
for (const byte c : cb0Cip) { fprintf(fd, "%02x ", c & 0xFF); } fputs("\n", fd);
for (const byte c : cb9Cip) { fprintf(fd, "%02x ", c & 0xFF); } fputs("\n", fd);
for (const byte c : ecbCip) { fprintf(fd, "%02x ", c & 0xFF); } fputs("\n", fd);
fclose(fd);
/* Decryption */
CFB_Mode<AES>::Decryption cfbDec; cfbDec.SetKey(key, 16, cfbP);
CBC_Mode<AES>::Decryption cb0Dec(key, 16, iv0);
CBC_Mode<AES>::Decryption cb9Dec(key, 16, iv9);
ECB_Mode<AES>::Decryption ecbDec(key, 16);
StreamTransformationFilter cfbStfD(cfbDec, new StringSink(cfbRec), StreamTransformationFilter::NO_PADDING);
StreamTransformationFilter cb0StfD(cb0Dec, new StringSink(cb0Rec), StreamTransformationFilter::ZEROS_PADDING);
StreamTransformationFilter cb9StfD(cb9Dec, new StringSink(cb9Rec), StreamTransformationFilter::PKCS_PADDING);
StreamTransformationFilter ecbStfD(ecbDec, new StringSink(ecbRec), StreamTransformationFilter::PKCS_PADDING);
cfbStfD.Put(reinterpret_cast<const unsigned char*>(cfbCip.c_str()), cfbCip.length());
cb0StfD.Put(reinterpret_cast<const unsigned char*>(cb0Cip.c_str()), cb0Cip.length());
cb9StfD.Put(reinterpret_cast<const unsigned char*>(cb9Cip.c_str()), cb9Cip.length());
ecbStfD.Put(reinterpret_cast<const unsigned char*>(ecbCip.c_str()), ecbCip.length());
cfbStfD.MessageEnd();
cb0StfD.MessageEnd();
cb9StfD.MessageEnd();
ecbStfD.MessageEnd();
/* Check if it matches original plaintext manually */
cout << "Decrypted Text (CFB Mode): " << cfbRec << '\n';
cout << "Decrypted Text (CBC Mode): " << cb0Rec << '\n';
cout << "Decrypted Text (CBC Mode): " << cb9Rec << '\n';
cout << "Decrypted Text (ECB Mode): " << ecbRec << '\n';
return 0;
}