-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset1_challenge8.cpp
39 lines (36 loc) · 1.26 KB
/
set1_challenge8.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
#include <iostream>
#include <fstream>
#include <string>
#include <map>
int main(int argc, char** argv) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " filename" << std::endl
<< "Test all lines of file for AES-128 ECB. Use 8.txt"
<< std::endl;
return 1;
}
std::ifstream f;
f.open(argv[1]);
if (!f) {
std::cerr << "Unable to open " << argv[1] << std::endl;
return 1;
}
std::string line;
size_t block_width = 32; // 32 hex characters = 16 bytes = 128 bits (block size of AES-128)
while (std::getline(f, line)) {
std::map<std::string, int> block_frequency;
size_t len = line.size();
size_t block_count = len/block_width;
for (size_t idx = 0; idx + block_width <= len; idx += block_width) {
++block_frequency[line.substr(idx, block_width)];
}
if (block_frequency.size() < block_count) {
std::cout << "Cipher with repeated blocks found: " << line << std::endl << std::endl;
for (auto it = block_frequency.cbegin(); it != block_frequency.cend(); ++it) {
std::cout << it->first << " : " << it->second << std::endl;
}
}
}
f.close();
return 0;
}