summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile3
-rw-r--r--lib.c20
-rw-r--r--lib.h2
-rw-r--r--task2.c27
-rw-r--r--test.sh9
5 files changed, 55 insertions, 6 deletions
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<strlen(string1)-3;i++) {
+ decode_four_base64_byte(&string1[i*4], &result[i*3]);
+ }
+ // TODO handle = in base64 at the end
+}
+
static char string_to_hex_map[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
0x0E, 0x0F};
@@ -203,12 +213,12 @@ static void two_char_hex(char *start, char *result)
* @param key
* @param result buffer where the result is stored
*/
-void xor_string(char *str1, char *key, char *result)
+void xor_string(char *str1, char *key, char *result, int length_key, int length_str1)
{
int i, j;
- int length_key = strlen(key);
+ //int length_key = strlen(key);
- for(i=0, j=0;i<strlen(str1);i++,j++) {
+ for(i=0, j=0;i<length_str1;i++,j++) {
if (j >= 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 <ctype.h>
-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