From 00ca660bedb23d7c461cbc6748f5dbc4f4f9f507 Mon Sep 17 00:00:00 2001 From: Zachary Vance Date: Wed, 24 May 2017 19:38:12 -0700 Subject: [PATCH] Solve Set 1, Problem 5 --- Makefile | 7 +++++-- io.h | 1 + set1p5.test.c | 34 ++++++++++++++++++++++++++++++++++ xor.c | 10 ++++++++++ xor.h | 1 + 5 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 set1p5.test.c diff --git a/Makefile b/Makefile index 36d9376..356a2a3 100644 --- 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 f0be7c1..b930907 100644 --- 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 index 0000000..ad0194d --- /dev/null +++ b/set1p5.test.c @@ -0,0 +1,34 @@ +#include +#include +#include +#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 33bfcc9..3a16263 100644 --- a/xor.c +++ b/xor.c @@ -1,5 +1,15 @@ +#include +#include "util.h" + void xor(char* buf1, char* buf2, char* buf3, int length) { int i; for(i=0; i