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
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)); }
--- /dev/null
+#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);
+ }
+}
+#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);
+}
void xor(char* buf1, char* buf2, char* buf3, int length);
+void repeating_xor(char* buf1, char* key, int keylength, char* buf3, int length);