-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAES-OFB-mode.cpp
180 lines (158 loc) · 8.38 KB
/
AES-OFB-mode.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
#include <iostream>
#include <chrono>
#include <cryptopp/cryptlib.h>
#include <cryptopp/hex.h>
#include <cryptopp/secblock.h>
#include <cryptopp/modes.h>
#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <cryptopp/osrng.h>
std::string aes_ofb_mode_encrypt(std::string &plain, CryptoPP::SecByteBlock key, CryptoPP::byte *iv) {
std::string cipher;
std::string output;
try {
CryptoPP::OFB_Mode<CryptoPP::AES>::Encryption e(key, key.size(), iv);
CryptoPP::StringSource(plain, true,
new CryptoPP::StreamTransformationFilter(e,
new CryptoPP::StringSink(cipher)
) //StreamTransformationFilter
); // StringSource
} catch (CryptoPP::Exception &exception) {
std::cerr << exception.what() << std::endl;
exit(1);
}
CryptoPP::StringSource(cipher, true,
new CryptoPP::HexEncoder(
new CryptoPP::StringSink(output)
) // HexEncoder
); // StringSource
return output;
}
std::string aes_ofb_mode_decrypt(std::string &encoded, CryptoPP::SecByteBlock key, CryptoPP::byte *iv) {
std::string cipher;
std::string output;
CryptoPP::StringSource(encoded, true,
new CryptoPP::HexDecoder(
new CryptoPP::StringSink(cipher)
) //HexDecoder
); //StringSource
try {
CryptoPP::OFB_Mode<CryptoPP::AES>::Decryption d(key, key.size(), iv);
CryptoPP::StringSource(cipher, true,
new CryptoPP::StreamTransformationFilter(d,
new CryptoPP::StringSink(output)
) //StreamTransformationFilter
); //StringSource
} catch (CryptoPP::Exception &exception) {
std::cerr << exception.what() << std::endl;
exit(1);
}
return output;
}
int main() {
std::string msg1 = "Lorem ipsum dolor sit amet conse";
std::string msg2 = "Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim,";
std::string msg3 = "Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, neque exercitationem? Et iusto veniam nostrum voluptatem dolor, m";
std::string msg4 = "Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, neque exercitationem? Et iusto veniam nostrum voluptatem dolor, maxime deleniti harum aperiam molestias animi quam assumenda ipsam repellat earum ab quae. Lorem ipsum dolor sit amet consectetur";
std::string msg5 = "Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, neque exercitationem? Et iusto veniam nostrum voluptatem dolor, maxime deleniti harum aperiam molestias animi quam assumenda ipsam repellat earum ab quae. Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, neque exercitationem? Et iusto veniam nostrum voluptatem dolor, maxime deleniti harum aperiam molestias animi quam assumenda ipsam repellat earum ab quae. Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, neque exerci";
std::string cipher;
std::chrono::_V2::system_clock::time_point start, end;
std::chrono::microseconds duration;
CryptoPP::AutoSeededRandomPool rnd;
CryptoPP::SecByteBlock key(CryptoPP::AES::DEFAULT_KEYLENGTH);
rnd.GenerateBlock(key, key.size());
CryptoPP::byte iv[ CryptoPP::AES::BLOCKSIZE ];
rnd.GenerateBlock(iv, sizeof(iv));
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 1000; i++) {
cipher = aes_ofb_mode_encrypt(msg1, key, iv);
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds> (end - start);
std::cout << "Message: " << msg1 << std::endl;
std::cout << "Cipher: " << cipher << std::endl;
std::cout << "Input 256 bits: " << duration.count() / 1000 << " microseconds" << std::endl << std::endl;
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 1000; i++) {
cipher = aes_ofb_mode_encrypt(msg2, key, iv);
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds> (end - start);
std::cout << "Message: " << msg2 << std::endl;
std::cout << "Cipher: " << cipher << std::endl;
std::cout << "Input 512 bits: " << duration.count() / 1000 << " microseconds" << std::endl << std::endl;
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 1000; i++) {
cipher = aes_ofb_mode_encrypt(msg3, key, iv);
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds> (end - start);
std::cout << "Message: " << msg3 << std::endl;
std::cout << "Cipher: " << cipher << std::endl;
std::cout << "Input 1024 bits: " << duration.count() / 1000 << " microseconds" << std::endl << std::endl;
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 1000; i++) {
cipher = aes_ofb_mode_encrypt(msg4, key, iv);
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds> (end - start);
std::cout << "Message: " << msg4 << std::endl;
std::cout << "Cipher: " << cipher << std::endl;
std::cout << "Input 2048 bits: " << duration.count() / 1000 << " microseconds" << std::endl << std::endl;
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 1000; i++) {
cipher = aes_ofb_mode_encrypt(msg5, key, iv);
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds> (end - start);
std::cout << "Message: " << msg5 << std::endl;
std::cout << "Cipher: " << cipher << std::endl;
std::cout << "Input 4096 bits: " << duration.count() / 1000 << " microseconds" << std::endl << std::endl;
std::cout << "########### decryption" << std::endl;
std::string recovered;
std::string cipher1 = aes_ofb_mode_encrypt(msg1, key, iv);
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 1000; i++) {
recovered = aes_ofb_mode_decrypt(cipher1, key, iv);
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds> (end - start);
std::cout << "Recovered: " << recovered << std::endl << std::endl;
std::cout << "Input 256 bits: " << duration.count() / 1000 << " microseconds" << std::endl << std::endl;
std::string cipher2 = aes_ofb_mode_encrypt(msg2, key, iv);
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 1000; i++) {
recovered = aes_ofb_mode_decrypt(cipher2, key, iv);
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds> (end - start);
std::cout << "Recovered: " << recovered << std::endl << std::endl;
std::cout << "Input 512 bits: " << duration.count() / 1000 << " microseconds" << std::endl << std::endl;
std::string cipher3 = aes_ofb_mode_encrypt(msg3, key, iv);
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 1000; i++) {
recovered = aes_ofb_mode_decrypt(cipher3, key, iv);
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds> (end - start);
std::cout << "Recovered: " << recovered << std::endl << std::endl;
std::cout << "Input 1024 bits: " << duration.count() / 1000 << " microseconds" << std::endl << std::endl;
std::string cipher4 = aes_ofb_mode_encrypt(msg4, key, iv);
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 1000; i++) {
recovered = aes_ofb_mode_decrypt(cipher4, key, iv);
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds> (end - start);
std::cout << "Recovered: " << recovered << std::endl << std::endl;
std::cout << "Input 2048 bits: " << duration.count() / 1000 << " microseconds" << std::endl << std::endl;
std::string cipher5 = aes_ofb_mode_encrypt(msg5, key, iv);
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 1000; i++) {
recovered = aes_ofb_mode_decrypt(cipher5, key, iv);
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds> (end - start);
std::cout << "Recovered: " << recovered << std::endl << std::endl;
std::cout << "Input 4096 bits: " << duration.count() / 1000 << " microseconds" << std::endl << std::endl;
}