From 9db81cb91674d45bbb4936a098f4e4fb1a5b45bd Mon Sep 17 00:00:00 2001 From: Benedict Date: Fri, 4 Mar 2016 12:42:08 +0100 Subject: reimplemented task2 --- Makefile | 3 +++ lib.c | 20 +++++++++++++++----- lib.h | 2 +- task2.c | 27 +++++++++++++++++++++++++++ test.sh | 9 +++++++++ 5 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 task2.c diff --git a/Makefile b/Makefile index 2178244..69b8615 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,9 @@ all: task1 task4 task5 task6 task1: $(CC) task1.c $(LIB) -o task1 +task2: + $(CC) task2.c $(LIB) -o task2 + task4: $(CC) task4.c $(LIB) -o task4 diff --git a/lib.c b/lib.c index 4d0d769..252deb2 100644 --- a/lib.c +++ b/lib.c @@ -90,7 +90,7 @@ static void three_bytes_to_base64(char * encode, int bytes_to_print, char *resul * Transform four base64 encoded characters back to three bytes */ -void decode_base64(char *string1, char *result) +void decode_four_base64_byte(char *string1, char *result) { char one, two, three, tmp; // first byte is first six bits of base64 one and 2 bit of base64 second @@ -113,6 +113,16 @@ void decode_base64(char *string1, char *result) printf("%c%c%c", one, two, three); } +void decode_base64(char *string1, char *result) +{ + int i; + + for(i=0;i= length_key) j = 0; @@ -364,7 +374,7 @@ int brute_force_single_byte_xor(char *string, int length, char* keys) for(i=1;i<255;i++) { single_byte_key = (char) i; - xor_string(string, &single_byte_key, xor_tmp); + xor_string(string, &single_byte_key, xor_tmp, 1, length); if (string_looks_like_it_is_human_language(xor_tmp, length)) { keys[number_found_keys++] = single_byte_key; printf("%s\n", xor_tmp); diff --git a/lib.h b/lib.h index b8d50d0..a0986e7 100644 --- a/lib.h +++ b/lib.h @@ -8,7 +8,7 @@ #include -void xor_string(char *str1, char *key, char *result); +void xor_string(char *str1, char *key, char *result, int length_key, int length_str1); void hex_binary_to_string(char *str1, char *result, int length); int decode_hex_string(char *encode, char* result); int encode_to_base64(char *encode, char *result); diff --git a/task2.c b/task2.c new file mode 100644 index 0000000..b675244 --- /dev/null +++ b/task2.c @@ -0,0 +1,27 @@ +#include "lib.h" + + +int main(int argc, char**argv) +{ + if (argc != 3) + return 1; + + + char *xor_string_tmp = malloc(strlen(argv[1])); + char *result = malloc(strlen(argv[1])); + char *str1 = malloc(strlen(argv[1])); + char *str2 = malloc(strlen(argv[2])); + + decode_hex_string(argv[1], str1); + decode_hex_string(argv[2], str2); + xor_string(str1, str2, xor_string_tmp, strlen(argv[2]), strlen(argv[1])); + hex_binary_to_string(xor_string_tmp, result, strlen(argv[1])); + printf("%s\n", result); + + free(xor_string_tmp); + free(result); + free(str1); + free(str2); + + return 0; +} diff --git a/test.sh b/test.sh index a728fa0..780a12b 100644 --- a/test.sh +++ b/test.sh @@ -28,5 +28,14 @@ EXCEPTED="SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t" test_compare_string "$OUTPUT" "$EXCEPTED" } +test_set1_challenge2() { + echo "test: set1, challenge 2:" + OUTPUT=$(./task2 1c0111001f010100061a024b53535009181c 686974207468652062756c6c277320657965) + EXCEPTED="746865206b696420646f6e277420706c6179" + + test_compare_string "$OUTPUT" "$EXCEPTED" +} + test_set1_challenge1 +test_set1_challenge2 test_set1_challenge5 -- cgit v1.2.3-70-g09d2