Skip to content

Instantly share code, notes, and snippets.

@breadchris
Last active August 29, 2015 14:17
Show Gist options
  • Save breadchris/3ec55c987fc025c31c31 to your computer and use it in GitHub Desktop.
Save breadchris/3ec55c987fc025c31c31 to your computer and use it in GitHub Desktop.
Break repeating-key XOR
//
// challenge_6.c
// Matasano Crypto Challenge
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char* HEX_LOOKUP = "0123456789abcdef";
int MIN_KEYSIZE = 2;
int MAX_KEYSIZE = 40;
int LINE_LENGTH = 60;
void formatAsHex(char* out, char* str) {
for (int i = 0; i < strlen(str); ++i) {
out[i*2] = HEX_LOOKUP[(str[i] & 0xf0) >> 4];
out[i*2+1] = HEX_LOOKUP[(str[i] & 0xf)];
}
}
void repeatedXOR(char* encrypted, char* text, char* key) {
for (int i = 0; i < strlen(text); i++) {
encrypted[i] = text[i] ^ key[i % strlen(key)];
}
}
void blockToSingleByte(char* byteBlocks[], char* str, unsigned int keysize) {
for (int i = 0; i < strlen(str); i++) {
byteBlocks[i % keysize][i / keysize] = str[i];
}
}
int keyedEditDistance(char* enc, unsigned int nBytes) {
if (strlen(enc) < 2 * nBytes) {
perror("[-] Not enough characters in given encrypted string");
return -1;
}
int distance = 0;
for (int i = 0; i < nBytes; ++i) {
int xor = enc[i] ^ enc[nBytes + i];
do {
if (xor & 0x1) distance++;
} while((xor = xor >> 1));
}
return distance;
}
unsigned int findKeysize(char* enc) {
unsigned int keysize = 0;
unsigned int smallestDistance = (unsigned int) -1;
unsigned int distance = 0;
for (int i = MIN_KEYSIZE; i < MAX_KEYSIZE; i++) {
if ((distance = keyedEditDistance(enc, i)) < smallestDistance) {
smallestDistance = distance;
keysize = i;
}
}
return keysize;
}
void base64Decode(char* out, char* str) {
}
void readFile(char* out, char* filename) {
char line[LINE_LENGTH];
FILE* f = fopen(filename, "r");
if (f == NULL) {
perror("[-] Could not open specified file");
exit(1);
}
while(fgets(line, 62, f) != NULL)
{
// realloc?
}
}
int main() {
// readFile()
// base64Decode()
// keysize = findKeysize()
// char* byteBlocks[keysize];
// for i in [0 : keysize - 1]:
// byteBlocks[i] = malloc(strlen(str) / keysize)
// blockToSingleByte(byteBlocks, str, keysize)
// for each block:
// find key
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment