Created
May 25, 2015 21:01
-
-
Save forrestbthomas/b3c7e7128e9ab8c5f653 to your computer and use it in GitHub Desktop.
Detect single-character XOR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
int most_freq_chars(char *decrypt_attempt) | |
{ | |
struct high_freq_characters { | |
int E; | |
int T; | |
int A; | |
int O; | |
int I; | |
int N; | |
int space; | |
}; | |
struct high_freq_characters hfq = {0,0,0,0,0,0}; | |
struct low_freq_characters { | |
int V; | |
int K; | |
int J; | |
int X; | |
int Q; | |
int Z; | |
}; | |
struct low_freq_characters lfq = {0,0,0,0,0,0}; | |
int j = 0; | |
for (j = 0; decrypt_attempt[j] != '\0'; j++) | |
{ | |
switch(decrypt_attempt[j]) | |
{ | |
case 'E': hfq.E++; break; | |
case 'e': hfq.E++; break; | |
case 'T': hfq.T++; break; | |
case 't': hfq.E++; break; | |
case 'A': hfq.A++; break; | |
case 'a': hfq.E++; break; | |
case 'O': hfq.O++; break; | |
case 'o': hfq.E++; break; | |
case 'I': hfq.I++; break; | |
case 'i': hfq.E++; break; | |
case 'N': hfq.N++; break; | |
case 'n': hfq.E++; break; | |
} | |
switch(decrypt_attempt[j]) | |
{ | |
case 'V': lfq.V--; break; | |
case 'v': lfq.V--; break; | |
case 'K': lfq.K--; break; | |
case 'k': lfq.V--; break; | |
case 'J': lfq.J--; break; | |
case 'j': lfq.V--; break; | |
case 'X': lfq.X--; break; | |
case 'x': lfq.V--; break; | |
case 'Q': lfq.Q--; break; | |
case 'q': lfq.V--; break; | |
case 'Z': lfq.Z--; break; | |
case 'z': lfq.V--; break; | |
} | |
} | |
int high_freq_total = hfq.E + hfq.T + hfq.A + hfq.O + hfq.I + hfq.N; | |
int low_freq_total = lfq.V + lfq.K + lfq.J + lfq.X + lfq.Q + lfq.Z; | |
int total = high_freq_total + low_freq_total; | |
return total; | |
} | |
int max = 0; | |
char result[128]; | |
char original_hex[128]; | |
void hex_to_int(char *cmd_line_arg) | |
{ | |
int i = 0; | |
int j = 0; | |
int hex_result; | |
char xor_string[strlen(cmd_line_arg) + 1]; | |
for (j = 0; j < 128; j++) | |
{ | |
int k = 0; | |
char xor_string[128]; | |
for (i = 0; cmd_line_arg[i] != '\0'; i+=2) | |
{ | |
char temp[3]; | |
int hex = 0; | |
temp[0] = cmd_line_arg[i]; | |
temp[1] = cmd_line_arg[i + 1]; | |
temp[2] = '\0'; | |
sscanf(temp, "%x", &hex); | |
int xor_i = hex ^ j; | |
char xor_c = xor_i + '\0'; | |
xor_string[k] = xor_c; | |
k++; | |
} | |
xor_string[k] = '\0'; | |
if (most_freq_chars(xor_string) > max) | |
{ | |
max = most_freq_chars(xor_string); | |
strcpy(result, xor_string); | |
strcpy(original_hex, cmd_line_arg); | |
} | |
} | |
} | |
int decrypt(char argv[]) | |
{ | |
hex_to_int(argv); | |
return 0; | |
} | |
int main() | |
{ | |
char * buffer = 0; | |
long length; | |
FILE *f; | |
f = fopen("challenge_four_data.txt", "r"); | |
if (f) | |
{ | |
fseek (f, 0, SEEK_END); | |
length = ftell (f); | |
fseek (f, 0, SEEK_SET); | |
buffer = malloc (length); | |
if (buffer) | |
{ | |
fread (buffer, 1, length, f); | |
} | |
fclose (f); | |
} | |
if (buffer) | |
{ | |
char line[128]; | |
int i = 0; | |
int j = 0; | |
for (i = 0; buffer[i] != '\0'; i++) | |
{ | |
line[j] = buffer[i]; | |
j++; | |
if (buffer[i] == '\n') | |
{ | |
j = 0; | |
decrypt(line); | |
} | |
} | |
} | |
printf(" max: %d\n result: %s\n original hex: %s\n", max, result, original_hex); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment