Skip to content

Instantly share code, notes, and snippets.

@forrestbthomas
Created May 25, 2015 19:50
Show Gist options
  • Save forrestbthomas/b011842bc77e2812408b to your computer and use it in GitHub Desktop.
Save forrestbthomas/b011842bc77e2812408b to your computer and use it in GitHub Desktop.
Single-byte XOR cipher
#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;
}
void hex_to_int(char *cmd_line_arg)
{
int i = 0;
int j = 0;
int max = 0;
int hex_result;
char xor_string[strlen(cmd_line_arg) + 1];
char result[128];
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);
}
}
printf("max: %d === result: %s\n", max, result);
}
int main(int argc, char *argv[])
{
hex_to_int(argv[1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment