diff options
| author | Benedict <benedict@0xb8000.de> | 2016-03-22 19:11:22 +0100 |
|---|---|---|
| committer | Benedict <benedict@0xb8000.de> | 2017-02-21 13:00:25 +0100 |
| commit | 4ed371cc378a3b579d46ed89a2677769a6d5ea24 (patch) | |
| tree | 9e9bb09f2798954540bc62d452258a74923c5e8d /set2 | |
| parent | b586255b0dc1d6940c4a552a7125c9875e8adba6 (diff) | |
completed set 2 challenge 11
Diffstat (limited to 'set2')
| -rw-r--r-- | set2/Makefile | 2 | ||||
| -rw-r--r-- | set2/task10.c | 4 | ||||
| -rw-r--r-- | set2/task11.c | 83 |
3 files changed, 88 insertions, 1 deletions
diff --git a/set2/Makefile b/set2/Makefile index 1f8f890..9c5f43e 100644 --- a/set2/Makefile +++ b/set2/Makefile @@ -9,6 +9,8 @@ task9: $(CC) $(CFLAGS) task9.c $(LIB) $(CLIBS) -o task9 task10: $(CC) $(CFLAGS) task10.c $(LIB) $(CLIBS) -o task10 +task11: + $(CC) $(CFLAGS) task11.c $(LIB) $(CLIBS) -o task11 task15: $(CC) $(CFLAGS) task15.c $(LIB) $(CLIBS) -o task15 clean: diff --git a/set2/task10.c b/set2/task10.c index fb8d0c1..19a7235 100644 --- a/set2/task10.c +++ b/set2/task10.c @@ -22,8 +22,10 @@ int main(int argc, char **argv) decoded_content_length = decode_base64(file_content, decoded_content); cleartext = malloc(decoded_content_length+1); // aes cbc - aes_cbc(decoded_content, decoded_content_length, cleartext, string_key, iv); + aes_cbc(decoded_content, decoded_content_length, cleartext, string_key, iv, 0); printf("%s\n", cleartext); + + free(decoded_content); free(cleartext); return 0; diff --git a/set2/task11.c b/set2/task11.c new file mode 100644 index 0000000..60483e0 --- /dev/null +++ b/set2/task11.c @@ -0,0 +1,83 @@ +#include "../lib/lib2.h" +#include "../lib/lib.h" + +int random_number_between(int min, int max) +{ + return (rand() % (max-min) + min); +} + + +int generate_random_bytes(char *buf, int length_key_bytes) +{ + int random_number; + int i; + for(i=0;i<length_key_bytes;i++) { + buf[i] = (char) random_number_between(0,255); + } + +} + +char *encrypt_with_random_bytes(char *toencrypt, int length, int ecb) +{ + int toappend_before = random_number_between(5,10); + int toappend_after= random_number_between(5,10); + + char random_bytes[10]; + char key[16]; + + char *result = malloc(length+toappend_before+toappend_after+1); + char *ciphertext = malloc(length+toappend_before+toappend_after+1); + + generate_random_bytes(random_bytes, toappend_before); + + memcpy(result, random_bytes, toappend_before); + memcpy(&result[toappend_before], toencrypt, length); + + generate_random_bytes(random_bytes, toappend_after); + + memcpy(&result[length+toappend_before], random_bytes, toappend_after); + + generate_random_bytes(key, 16); + + + if(ecb) + aes_ecb(result, (length+toappend_before+toappend_after), ciphertext, key, 128, 1); + else { + char iv[16]; + memset(iv, 0, 16); + aes_cbc(result, (length+toappend_before+toappend_after), ciphertext, key, iv, 1); + } + + return ciphertext; +} + +int main(int argc, char **argv) +{ + // set seed +// srand(1); + int i; + // encrypt ecb mode + char *cleartext = "Ok, dann mach ich mal einen richtigen Satz. Vielleicht ist das ECB mit so vielen gleichen Bloecken total ueberfordert. Oder der Satz muss einfach laenger sein. Man brauch also wirklich wohl erstmal ein bisschen Text, bevor man ECB erkennen kann"; + char *ciphertext; + char *ciphertext_cbc; + char iv[16]; + + memset(iv, 0, 16); + + ciphertext = encrypt_with_random_bytes(cleartext, strlen(cleartext), 1); + ciphertext_cbc = encrypt_with_random_bytes(cleartext, strlen(cleartext), 0); + //aes_ecb(cleartext, strlen(cleartext), ciphertext, "BUMMS", 128, 1); + //aes_cbc(cleartext, strlen(cleartext), ciphertext_cbc, "BUMMS", iv, 1); + + if(string_is_ecb_encrypted(ciphertext_cbc, strlen(cleartext), 16)) + printf("Das ist wohl ECB (ist es aber nicht!)\n"); + + if(string_is_ecb_encrypted(ciphertext, strlen(cleartext), 16)) + printf("Das ist wohl ECB (richtig)\n"); +/* + for(i=0;i<strlen(cleartext);i++) + printf("%c", ciphertext_cbc[i]); + +*/ + return 0; +} |
