-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset4_challenge27.cpp
40 lines (31 loc) · 1.3 KB
/
set4_challenge27.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
#include "cryptopals_uri.h"
#include <sstream>
#include <iostream>
int main(int argc, char ** argv) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " seed" << std::endl;
std::cerr << "CBC bitflipping attack" << std::endl;
return 1;
}
std::istringstream ss(argv[1]);
int seed;
if (!(ss >> seed)) {
std::cerr << "Invalid number: " << argv[1] << std::endl;
return 1;
}
init_random_encrypt(seed);
std::string s("3admin5true");
byte_array cipher = uri_encrypt_cbc_matching_iv(s);
byte_array cipher_1 = sub_byte_array(cipher, 0, 16);
byte_array zero_block = alloc_byte_array(16);
// need to end in full cipher in order to pass PKCS#7 padding check
byte_array spoof_cipher = append_three_byte_arrays(cipher_1, zero_block, cipher);
byte_array spoof_decrypt = uri_decrypt_cbc_matching_iv(spoof_cipher);
byte_array plain_1 = sub_byte_array(spoof_decrypt, 0, 16);
byte_array plain_3 = sub_byte_array(spoof_decrypt, 32, 48);
byte_array key = xor_byte_arrays(NO_BA, plain_1, plain_3);
printf("%s the key!\n", guess_key(key) ? "Cracked" : "Did not crack");
free_byte_arrays(cipher, cipher_1, zero_block, spoof_cipher, spoof_decrypt, plain_1, plain_3, key, NO_BA);
cleanup_random_encrypt();
return 0;
}