diff options
Diffstat (limited to 'set3/task20.c')
| -rw-r--r-- | set3/task20.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/set3/task20.c b/set3/task20.c new file mode 100644 index 0000000..7ab24de --- /dev/null +++ b/set3/task20.c @@ -0,0 +1,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); + } +} |
