diff options
Diffstat (limited to 'set3')
| -rw-r--r-- | set3/Makefile | 8 | ||||
| -rw-r--r-- | set3/task20.c | 64 |
2 files changed, 70 insertions, 2 deletions
diff --git a/set3/Makefile b/set3/Makefile index d3b3f03..a6ed5fc 100644 --- a/set3/Makefile +++ b/set3/Makefile @@ -3,12 +3,16 @@ CC=gcc CFLAGS := -g $(CFLAGS) CLIBS=`pkg-config --cflags --libs libcrypto` -all: task17 task18 +all: task17 task18 task19 task20 task17: $(CC) $(CFLAGS) task17.c $(LIB) $(CLIBS) -o task17 task18: $(CC) $(CFLAGS) task18.c $(LIB) $(CLIBS) -o task18 +task19: + $(CC) $(CFLAGS) task19.c $(LIB) $(CLIBS) -o task19 +task20: + $(CC) $(CFLAGS) task20.c $(LIB) $(CLIBS) -o task20 clean: - rm task17 task18 + rm task17 task18 task19 task20 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); + } +} |
