summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenedict <benedict@0xb8000.de>2016-03-01 19:36:31 +0100
committerBenedict <benedict@0xb8000.de>2017-02-21 13:00:24 +0100
commit5b58fc19fafa20a1e24f48e011ddc36d36dfe089 (patch)
tree0f1a41314206e7b1b34eb2ea42c2184368c5d63e
matanso: first challenge completed, second partly
-rw-r--r--main.c0
-rw-r--r--set1.h0
-rw-r--r--task1_hex_to_base64.c217
-rw-r--r--task2_fixed_xor.c0
4 files changed, 217 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/main.c
diff --git a/set1.h b/set1.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/set1.h
diff --git a/task1_hex_to_base64.c b/task1_hex_to_base64.c
new file mode 100644
index 0000000..53b98e3
--- /dev/null
+++ b/task1_hex_to_base64.c
@@ -0,0 +1,217 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+static const unsigned char base64_encode[] =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789+/";
+
+static const unsigned char hex_encode[] =
+ "0123456789abcdef";
+
+/*
+ * This functions accepts a string and returns it base64 encoded
+*/
+void three_bytes_to_base64(char * encode, int bytes_to_print, char *result)
+{
+ unsigned char one, two, three, four;
+ unsigned char tmp;
+ char ret[4];
+
+ // first six bits
+ one = encode[0] >> 2;
+ ret[0] = base64_encode[one];
+ if(bytes_to_print-- > 0)
+ result[0] = ret[0];
+ // second six bits
+ // two last bits of first byte
+ tmp = encode[0] & 0x03;
+ two = encode[1] >> 4;
+ tmp = tmp << 4;
+ two = tmp | two;
+ ret[1] = base64_encode[two];
+ if(bytes_to_print-- > 0)
+ result[1] = ret[1];
+ // second six bits
+ // two last bits of first byte
+ // third six bits
+ tmp = encode[1] & 0x0F;
+ tmp = tmp << 2;
+ three = encode[2] & 0xC0;
+ three = three >> 6;
+ three = tmp | three;
+ ret[2] = base64_encode[three];
+ if(bytes_to_print-- > 0)
+ result[2] = ret[2];
+ // last six bit11s
+ four = encode[2] & 0x3F;
+ ret[3] = base64_encode[four];
+ ret[4] = '\0';
+ if(bytes_to_print-- > 0)
+ result[3] = ret[3];
+ }
+
+
+
+
+static char string_to_hex_map[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
+ 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
+ 0x0E, 0x0F};
+/*
+ * This function converts the first char of a string into its corresponding
+ * hex value
+ *
+ * @return On Sucess return 0, if char is no valid hex value returns -1
+ */
+int hex_char_to_bit(char toEncode)
+{
+ char encode;
+ switch(toEncode) {
+ case '0':
+ encode = string_to_hex_map[0];
+ break;
+ case '1':
+ encode = string_to_hex_map[1];
+ break;
+ case '2':
+ encode = string_to_hex_map[2];
+ break;
+ case '3':
+ encode = string_to_hex_map[3];
+ break;
+ case '4':
+ encode = string_to_hex_map[4];
+ break;
+ case '5':
+ encode = string_to_hex_map[5];
+ break;
+ case '6':
+ encode = string_to_hex_map[6];
+ break;
+ case '7':
+ encode = string_to_hex_map[7];
+ break;
+ case '8':
+ encode = string_to_hex_map[8];
+ break;
+ case '9':
+ encode = string_to_hex_map[9];
+ break;
+ case 'a':
+ encode = string_to_hex_map[10];
+ break;
+ case 'b':
+ encode = string_to_hex_map[11];
+ break;
+ case 'c':
+ encode = string_to_hex_map[12];
+ break;
+ case 'd':
+ encode = string_to_hex_map[13];
+ break;
+ case 'e':
+ encode = string_to_hex_map[14];
+ break;
+ case 'f':
+ encode = string_to_hex_map[15];
+ break;
+ default:
+ return -1;
+ }
+
+ return encode;
+
+}
+
+
+void two_char_hex(char *start, char *result)
+{
+ char first = hex_char_to_bit(start[0]);
+ char second = hex_char_to_bit(start[1]);
+
+ *result = first;
+ *result = *result << 4;
+ *result = *result | second;
+}
+
+int convert_hex_string_to_binary(char *encode, char* result)
+{
+ int i;
+ int length = strlen(encode);
+
+ for(i = 0;i< length/2;i++)
+ two_char_hex(&encode[i*2], &result[i]);
+
+ return i;
+}
+
+int convert_to_base64(char *encode, char *result)
+{
+ int length = strlen(encode);
+
+ int rounds = length / 3;
+ int bytes_last_round = length % 3;
+ int i;
+
+ for (i=0;i<rounds;i++) {
+ three_bytes_to_base64(encode + i*3,4, &result[i*4]);
+ }
+ // in der letzen runde nicht mehr alle ausgeben
+ // nur noch 3-leftover
+ if (bytes_last_round > 0) {
+ three_bytes_to_base64(encode + i*3, 1+bytes_last_round, &result[i*4]);
+
+ for(i=0;i<(3-bytes_last_round);i++)
+ result[i*4+(1+bytes_last_round)] = '=';
+ }
+
+}
+
+void xor_string(char *str1, char* str2, char *result) {
+ int length = strlen(str1);
+ int i;
+ for(i=0;i<length;i++)
+ result[i] = str1[i] ^ str2[i];
+
+ return;
+
+}
+
+void hex_binary_to_string(char *str1, char *result)
+{
+ int i;
+ int length = strlen(str1);
+ int tmp = 0;
+
+ for(i=0;i<length;i++) {
+ tmp = str1[i] & 0xF0;
+ tmp = tmp >> 4;
+ result[i] = hex_encode[tmp];
+ tmp = str1[i] & 0x0F;
+ result[i+1] = hex_encode[tmp];
+ }
+}
+
+int main(int argc, char **argv)
+{
+ if (argc == 3 ) {
+ int length = strlen(argv[1]);
+ char *tmp = malloc(length);
+ char *tmp2 = malloc(length);
+ char *base64= malloc(length);
+ char *__xor_string = malloc(length);
+ char *back_hex = malloc(length);
+ int i = convert_hex_string_to_binary(argv[1], tmp);
+ int j = convert_hex_string_to_binary(argv[2], tmp2);
+ xor_string(tmp, tmp2, __xor_string);
+ hex_binary_to_string(tmp2, back_hex);
+ printf("%s\n", tmp);
+ printf("%s\n", tmp2);
+ printf("%s\n", argv[2]);
+ printf("%s\n", back_hex);
+ printf("%s\n", __xor_string);
+ convert_to_base64(tmp, base64);
+ //printf("%s\n", base64);
+ }
+}
diff --git a/task2_fixed_xor.c b/task2_fixed_xor.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/task2_fixed_xor.c