]> git.za3k.com Git - cryptopals.git/commitdiff
Solve Set 1, Problem 5
authorZachary Vance <za3k@za3k.com>
Thu, 25 May 2017 02:38:12 +0000 (19:38 -0700)
committerZachary Vance <za3k@za3k.com>
Thu, 25 May 2017 02:38:12 +0000 (19:38 -0700)
Makefile
io.h
set1p5.test.c [new file with mode: 0644]
xor.c
xor.h

index 36d93766ace700eb36264b5cbba173fbc5f44729..356a2a364ed5ee665b334eb163d0c008ee9d2af4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,17 +6,20 @@ all: libs
 libs: io.o xor.o util.o english.o xor_decrypt.o
 set1p1.test: set1p1.test.c io.o
        $(CC) -o $@ $? $(CFLAGS)
-set1p2.test: set1p2.test.c io.o xor.o
+set1p2.test: set1p2.test.c io.o xor.o util.o
        $(CC) -o $@ $? $(CFLAGS)
 set1p3.test: set1p3.test.c io.o xor_decrypt.o english.o util.o xor.o
        $(CC) -o $@ $? $(CFLAGS)
 set1p4.test: set1p4.test.c io.o xor_decrypt.o english.o util.o xor.o
        $(CC) -o $@ $? $(CFLAGS)
-test: set1p1.test set1p2.test set1p3.test set1p4.test
+set1p5.test: set1p5.test.c io.o util.o xor.o
+       $(CC) -o $@ $? $(CFLAGS)
+test: set1p1.test set1p2.test set1p3.test set1p4.test set1p5.test
        ./set1p1.test >/dev/null || echo "Set 1, Problem 1: Failed"
        ./set1p2.test >/dev/null || echo "Set 1, Problem 2: Failed"
        ./set1p3.test >/dev/null || echo "Set 1, Problem 3: Failed"
        ./set1p4.test >/dev/null || echo "Set 1, Problem 4: Failed"
+       ./set1p5.test >/dev/null || echo "Set 1, Problem 5: Failed"
 clean:
        rm -f *.o a.out *.test
 %.o: %.c
diff --git a/io.h b/io.h
index f0be7c1593463048a9c2ff5c6693af4332d5d9f3..b93090724a0e59756686db173207accbb5449f38 100644 (file)
--- a/io.h
+++ b/io.h
@@ -4,3 +4,4 @@ void encode_base64(const char* src_bytes, int src_size, char* dest);
 void encode_hex(const char* src_bytes, int src_size, char* dest);
 char* printable_buffer(char* buffer, int length);
 void print_buffer(char* buffer, int length);
+#define PRINT_BUFFER(BUF) { printf("Buffer BUF : ");print_buffer(BUF,sizeof(BUF)); }
diff --git a/set1p5.test.c b/set1p5.test.c
new file mode 100644 (file)
index 0000000..ad0194d
--- /dev/null
@@ -0,0 +1,34 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "io.h"
+#include "util.h"
+#include "xor.h"
+
+int main() {
+  char plaintext[75]="Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal";
+  char key[4]="ICE";
+  char expected[74];
+  char expected_hex[sizeof(expected)*2+1]="0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f";
+  int size;
+  decode_hex(expected_hex, expected, &size);
+  if (sizeof(plaintext)-1 != sizeof(expected) || sizeof(expected) != size) {
+     printf("Plaintext size: %d\n", sizeof(plaintext)-1);
+     printf("Expected size (expected): %d\n", sizeof(expected));
+     printf("Expected size (decoded): %d\n", size);
+     printf("Size mismatch, aborting.\n");
+     exit(1);
+  }
+
+  char ciphertext[75];
+  repeating_xor(plaintext, key, 3, ciphertext, sizeof(expected));
+  if (memcmp(ciphertext, expected, sizeof(expected))==0) {
+    printf("Repeating-key xor: success\n");
+    exit(0);
+  } else {
+    printf("Repeating-key xor: failed\n");
+    PRINT_BUFFER(ciphertext);
+    PRINT_BUFFER(expected);
+    exit(1);
+  }
+}
diff --git a/xor.c b/xor.c
index 33bfcc91f1c60320409cbf13d111959bbc5a21a5..3a16263a52a4b24f4cfd2c9f02d619049c7087c3 100644 (file)
--- a/xor.c
+++ b/xor.c
@@ -1,5 +1,15 @@
+#include <stdlib.h>
+#include "util.h"
+
 void xor(char* buf1, char* buf2, char* buf3, int length) {
   int i;
   for(i=0; i<length; i++)
     buf3[i] = buf1[i] ^ buf2[i];
 }
+
+void repeating_xor(char* buf1, char* key, int keylength, char* buf3, int length) {
+  char *buf2 = malloc(length);
+  fill(key, keylength, buf2, length);
+  xor(buf1, buf2, buf3, length);
+  free(buf2);
+}
diff --git a/xor.h b/xor.h
index b4c7ba41ebcd7a815fb2f726a043556d015da3b1..0f037897e19fa708cd2f6f69cc4cab29602cb7f4 100644 (file)
--- a/xor.h
+++ b/xor.h
@@ -1 +1,2 @@
 void xor(char* buf1, char* buf2, char* buf3, int length);
+void repeating_xor(char* buf1, char* key, int keylength, char* buf3, int length);