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
|
#include "../lib/lib.h"
#include "../lib/lib2.h"
#include "../lib/lib3.h"
#include <time.h>
#define CHALLENGE20_FILE_NR 60
int main()
{
int i;
char **file = malloc(sizeof(char *)*CHALLENGE20_FILE_NR);
int length_file[CHALLENGE20_FILE_NR];
int length[CHALLENGE20_FILE_NR];
char **plaintext= malloc(sizeof(char *)*CHALLENGE20_FILE_NR);
char **ciphertext= malloc(sizeof(char *)*CHALLENGE20_FILE_NR);
char filename[] = "./task20_data/task20_00";
char nonce[16];
int min_length = 9999999;
generate_random_bytes(key, 16);
generate_random_bytes(nonce, 16);
for(i=0;i<CHALLENGE20_FILE_NR;i++) {
filename[strlen(filename)-1] = ((i % 10)+ '0');
filename[strlen(filename)-2] = ((i / 10) + '0');
length_file[i] = read_base64_file(filename, &file[i]);
plaintext[i] = malloc(length_file[i]);
length[i] = decode_base64(file[i], plaintext[i]);
if(length[i] < min_length)
min_length = length[i];
ciphertext[i] = malloc(length[i]);
aes_ctr(plaintext[i], length[i], ciphertext[i], key, nonce);
}
printf("smallest string : %i\n", min_length);
// let the cracking begin
// merge all the chunks and then do the vingere chiffre break
// so we have 60 blocks and the key length is min_length
// no need
char *merged = malloc(min_length*CHALLENGE20_FILE_NR);
for(i=0;i<CHALLENGE20_FILE_NR;i++) {
memcpy(&merged[i*min_length], ciphertext[i], min_length);
}
char **blocks = transpose_blocks(merged, min_length,
(min_length*CHALLENGE20_FILE_NR));
// run the normal ceasr chiffre attack
char restored_keystream[min_length];
struct key_and_freq tmp_NOT_USED_HERE;
for(i=0;i<min_length;i++) {
restored_keystream[i] = brute_force_single_byte_xor(
blocks[i], min_length, &tmp_NOT_USED_HERE);
}
// xor ciphertext against keystream
char *restored_plaintext = malloc(min_length);
for(i=0;i<CHALLENGE20_FILE_NR;i++) {
xor_string(ciphertext[i], restored_keystream, restored_plaintext,
min_length, min_length);
printf("%s\n", plaintext[i]);
printf("%s\n", restored_plaintext);
}
}
|