Last active
May 19, 2020 15:24
-
-
Save tlindner/6bdbbfaf5a1d563016f40992cca05a16 to your computer and use it in GitHub Desktop.
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 <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <ctype.h> | |
// Unknown keys. | |
const unsigned char cypher_text[] = { | |
0xEC,0xDD,0xF3,0xB5,0xD9,0x9D,0xA6, // l]s5Y.& | |
0xAF,0xEF,0xC7,0x8F,0xDE,0x8F,0xCA,0xE7, // /oG.^.Jg | |
0xE5,0xB1,0xEF,0xEE,0xF0,0xB7,0xED,0xD6, // e1onp7mV | |
0xED,0xBF,0x9D,0xA4,0xBC,0xDB,0xC2,0xAF, // m?.$<[B/ | |
0xE3,0xD6,0xC2,0xF0,0xBC,0xB9,0xEE,0xF5, // cVBp<9nu | |
0xE7,0xD4,0xDA,0xC3,0xE8,0xB8,0xA2,0xDF, // gTZCh8"_ | |
0xC4,0x84,0xA6,0xF2,0xE6,0xED,0xC6,0xB1, // D.&rfmF1 | |
0xD0,0xED,0xF7,0xDA,0xF4,0xB7,0xEB,0xE8, // PmwZt7kh | |
0xED,0xBA,0xC7,0xD0,0xAF,0xC2,0x8F,0xD9, // m:GP/B.Y | |
0xE3,0xF0,0xD1,0xF0,0xAC,0xEE,0xD9,0xB9, // cpQp,nY9 | |
0xDE,0xE9,0xFA,0xDD,0xA0,0x22,0x0D,0x4F, // ^iz] ".O | |
0x55,0x2E // U | |
}; | |
// known keys: AB & BC | |
// const unsigned char cypher_text[] = { | |
// 0xD1,0xD4,0xDA,0xA5,0xCC,0xD8,0xA3, // QTZ%LX# | |
// 0xD9,0xCB,0xCA,0xA3,0xD9,0xCC,0xD2,0xC8, // YKJ#YLRH | |
// }; | |
#define key_size 8 | |
// Keyspace: A-Z * key_size characters | |
int a_length = 0; | |
int b_length = 0; | |
void increment_builder_a( char *i, int pos) | |
{ | |
if( pos == -1 ) | |
{ | |
a_length++; | |
i[a_length] = 'A'; | |
return; | |
} | |
i[pos] += 1; | |
if( i[pos] > 'Z' ) | |
{ | |
i[pos] = 'A'; | |
increment_builder_a( i, pos - 1); | |
} | |
} | |
void increment_builder_b( char *i, int pos) | |
{ | |
if( pos == -1 ) | |
{ | |
b_length++; | |
i[b_length] = 'A'; | |
return; | |
} | |
i[pos] += 1; | |
if( i[pos] > 'Z' ) | |
{ | |
i[pos] = 'A'; | |
increment_builder_b( i, pos - 1); | |
} | |
} | |
void print_key( char *key, int length ) | |
{ | |
for( int i=0; i<=length; i++ ) | |
{ | |
printf( "%c", key[i] ); | |
} | |
} | |
int main(int argc, char *argv[] ) | |
{ | |
unsigned long k=0; | |
if( argc != 3 ) | |
{ | |
printf( "Key Checker\n"); | |
printf( "Key space A-Z\n"); | |
printf( "Built for key length: %d\n", key_size); | |
printf( "usage: %s <start key 1> <start key 2>\n\n", argv[0]); | |
return 0; | |
} | |
char a[key_size+1]; | |
char b[key_size+1]; | |
a_length = strlen(argv[1])-1; | |
b_length = strlen(argv[2])-1; | |
for( int i=0; i<strlen(argv[1]); i++ ) | |
{ | |
a[i] = toupper(argv[1][i]); | |
if( a[i] < 'A' || a[i] > 'Z') | |
{ | |
printf( "Character %c not in key space.\n", argv[1][i]); | |
return 0; | |
} | |
} | |
for( int i=0; i<strlen(argv[2]); i++ ) | |
{ | |
b[i] = toupper(argv[2][i]); | |
if( b[i] < 'A' || b[i] > 'Z') | |
{ | |
printf( "Character %c not in key space.\n", argv[2][i]); | |
return 0; | |
} | |
} | |
printf( "Starting with key: " ); | |
print_key( a, a_length ); | |
printf( " & "); | |
print_key( b, b_length ); | |
printf("\n"); | |
int result[sizeof cypher_text]; | |
while( 1 ) | |
{ | |
// on my computer report key check ever 30 minutes | |
if( ((k++) % (5470000 * 6 * 30)) == 0 ) | |
{ | |
printf( "k=%lu: ", k ); | |
print_key( a, a_length ); | |
printf( " & "); | |
print_key( b, b_length ); | |
printf("\n"); | |
} | |
int j=0, i=0, k=0; | |
while( j < sizeof cypher_text) | |
{ | |
result[j] = cypher_text[j] - a[i]; | |
if( result[j] < 0 ) result[j] += 255; | |
result[j] = result[j] - b[k]; | |
if( result[j] < 0 ) result[j] += 255; | |
j++; | |
k++; | |
i++; | |
if( i==a_length+1) i=0; | |
if( k==b_length+1) k=0; | |
} | |
for(int l=0; l<(sizeof cypher_text)-4; l++ ) | |
{ | |
if( result[l] == 'T' && result[l+1] == 'H' && result[l+2] == 'E' && result[l+3] == ' ') | |
{ | |
printf( "Find: " ); | |
print_key( a, a_length ); | |
printf( " & "); | |
print_key( b, b_length ); | |
printf(": "); | |
for( int m=0; m<(sizeof cypher_text); m++ ) | |
{ | |
printf( "%c", isprint(result[m]) ? result[m] : '.' ); | |
} | |
printf( "\n" ); | |
break; | |
} | |
} | |
increment_builder_a( a, a_length ); | |
if( a_length == key_size ) | |
{ | |
a_length = 0; | |
increment_builder_b( b, b_length ); | |
if( b_length == key_size ) | |
{ | |
break; | |
} | |
} | |
} | |
printf(" last key searched: "); | |
print_key( a, a_length ); | |
printf( " & "); | |
print_key( b, b_length ); | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment