@@ -1,11 +1,16 @@ | |||||
INC=. | INC=. | ||||
all: | all: | ||||
mkdir -p obj | mkdir -p obj | ||||
g++ -g -I${INC} -c utils/hex_to_base64.cpp -o obj/hex_to_base64.o | |||||
g++ -g -I${INC} -c utils/utils_tester.cpp -o obj/utils_tester.o | |||||
g++ -g -I${INC} -c utils/common.c -o obj/common.o | |||||
g++ -g -I${INC} -c utils/base64.cpp -o obj/base64.o | |||||
g++ -g -I${INC} -c utils/runner.cpp -o obj/utils_runner.o | |||||
g++ -g -I${INC} -c utils/xor.c -o obj/xor.o | g++ -g -I${INC} -c utils/xor.c -o obj/xor.o | ||||
g++ -g -I${INC} -c set1/xor_char_finder.cpp -o obj/xor_char_finder.o | g++ -g -I${INC} -c set1/xor_char_finder.cpp -o obj/xor_char_finder.o | ||||
g++ -g -I${INC} -c utils/hamming.c -o obj/hamming.o | g++ -g -I${INC} -c utils/hamming.c -o obj/hamming.o | ||||
g++ -g -lpthread -o utils_tester obj/hex_to_base64.o obj/utils_tester.o obj/xor_char_finder.o obj/xor.o obj/hamming.o | |||||
g++ -g -I${INC} -c set1/runner.cpp -o obj/set1_runner.o | |||||
g++ -g -I${INC} -c main.cpp -o obj/main.o | |||||
g++ -g -lpthread -o main obj/common.o obj/base64.o obj/xor_char_finder.o obj/xor.o \ | |||||
obj/hamming.o obj/utils_runner.o \ | |||||
obj/set1_runner.o obj/main.o | |||||
clean: | clean: | ||||
rm -rf obj utils_tester | |||||
rm -rf obj |
@@ -0,0 +1,8 @@ | |||||
#include "set1/runner.h" | |||||
#include "utils/runner.h" | |||||
int main() | |||||
{ | |||||
UTILS::run(); | |||||
SET1::run(); | |||||
} |
@@ -0,0 +1,135 @@ | |||||
#include <stdlib.h> | |||||
#include <assert.h> | |||||
#include <stdio.h> | |||||
#include <string.h> | |||||
#include "set1/xor_char_finder.h" | |||||
#include "utils/common.h" | |||||
#include "utils/base64.h" | |||||
#include "utils/xor.h" | |||||
#include "utils/hamming.h" | |||||
void set1_challenge3_test() | |||||
{ | |||||
static const char ciphertext[] = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"; | |||||
struct frequency_t max_score; | |||||
// ----------------------------------------------------------------------------- | |||||
// | |||||
// 1. Convert ASCII ciphertext to HEX | |||||
// | |||||
// ----------------------------------------------------------------------------- | |||||
const int ciphertext_len = strlen(ciphertext) / 2; | |||||
unsigned char ciphertext_xor[ciphertext_len]; | |||||
unsigned char* p_ciphertext_xor = ciphertext_xor; | |||||
convert_string_to_hex(ciphertext, ciphertext_len*2, p_ciphertext_xor); | |||||
xor_char_finder(p_ciphertext_xor, max_score, ciphertext_len); | |||||
assert(max_score.letter == 'X'); | |||||
} | |||||
void set1_challange4_test() | |||||
{ | |||||
FILE* fp; | |||||
char* line=NULL; | |||||
size_t len=0; | |||||
int line_nb = 0; | |||||
unsigned char hexdump[30]; | |||||
unsigned char* p_hexdump = hexdump; | |||||
fp=fopen("etc/set1_t4_input.txt", "r"); | |||||
if(fp==NULL) | |||||
{ | |||||
fprintf(stderr, "etc/set1_t4_input.txt not found"); | |||||
return; | |||||
} | |||||
while( getline(&line, &len, fp) != -1 ) | |||||
{ | |||||
++line_nb; | |||||
// printf("%s",line); | |||||
struct frequency_t max_score; | |||||
max_score.letter=0x0; | |||||
max_score.score = 0; | |||||
// ----------------------------------------------------------------------------- | |||||
// | |||||
// 1. Convert ASCII ciphertext to HEX | |||||
// | |||||
// ----------------------------------------------------------------------------- | |||||
const int ciphertext_len = strlen(line) / 2; | |||||
unsigned char ciphertext_xor[ciphertext_len]; | |||||
unsigned char* p_ciphertext_xor = ciphertext_xor; | |||||
convert_string_to_hex(line, ciphertext_len*2, p_ciphertext_xor); | |||||
xor_char_finder(p_ciphertext_xor, max_score, ciphertext_len); | |||||
if(max_score.letter != 0x00) | |||||
{ | |||||
// printf("%d -> %c\n", line_nb, max_score.letter); | |||||
convert_string_to_hex(line, 60, p_hexdump); | |||||
for(int i=0; i<30;++i) | |||||
{ | |||||
// printf("%c", hexdump[i]^max_score.letter); | |||||
} | |||||
// printf("\n"); | |||||
} | |||||
free(line); line=0; len=0; | |||||
} | |||||
free(line); | |||||
} | |||||
void set1_challenge_5_test() | |||||
{ | |||||
static const char ch1[] = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"; | |||||
static const char xor_val[] = "ICE"; | |||||
static const unsigned char expected[] = { | |||||
0x0b,0x36,0x37,0x27,0x2a,0x2b,0x2e,0x63,0x62,0x2c,0x2e,0x69,0x69,0x2a,0x23,0x69, | |||||
0x3a,0x2a,0x3c,0x63,0x24,0x20,0x2d,0x62,0x3d,0x63,0x34,0x3c,0x2a,0x26,0x22,0x63, | |||||
0x24,0x27,0x27,0x65,0x27,0x2a,0x28,0x2b,0x2f,0x20,0x43,0x0a,0x65,0x2e,0x2c,0x65, | |||||
0x2a,0x31,0x24,0x33,0x3a,0x65,0x3e,0x2b,0x20,0x27,0x63,0x0c,0x69,0x2b,0x20,0x28, | |||||
0x31,0x65,0x28,0x63,0x26,0x30,0x2e,0x27,0x28,0x2f}; | |||||
unsigned char o_ch1[256]; | |||||
memset(o_ch1, '\0', 256); | |||||
unsigned char* po_ch1 = o_ch1; | |||||
xor_repeatedly(xor_val, ch1, po_ch1); | |||||
for(unsigned i=0; i<sizeof(expected)/sizeof(unsigned char); ++i) | |||||
assert( expected[i] == po_ch1[i] ); | |||||
} | |||||
void set1_challenge_6_test() | |||||
{ | |||||
char buf[1024*1024]; // 1 MB | |||||
unsigned char hex_buf[1024*1024]; | |||||
char* pbuf=buf; | |||||
unsigned char* p_hex_buf=hex_buf; | |||||
unsigned long len=read_file_to_buffer("etc/set1_t6.txt", pbuf); | |||||
buf[len]='\0'; | |||||
len=base64_to_hex(buf, len, p_hex_buf); | |||||
for(int i=0; i<len; ++i) | |||||
buf[i]=hex_buf[i]; | |||||
buf[len]='\0'; | |||||
// guess keysize | |||||
int keysize = choose_min_block_size(buf, strlen(buf), 2); | |||||
unsigned char* slide = new unsigned char[strlen(buf)/keysize]; | |||||
for(int j=0; j<keysize; ++j) | |||||
{ | |||||
memset(slide, '\0', strlen(buf)/keysize); | |||||
int counter = 0; | |||||
for(int i=0; i+j<strlen(buf); i+=keysize, counter++) | |||||
{ | |||||
slide[counter] = hex_buf[i+j]; | |||||
} | |||||
struct frequency_t max_score; | |||||
xor_char_finder(slide, max_score, counter); | |||||
printf("> %c %x\n", max_score.letter, max_score.letter); | |||||
} | |||||
} |
@@ -0,0 +1,21 @@ | |||||
#ifndef __set1_runner__ | |||||
#define __set1_runner__ | |||||
void set1_challenge3_test(); | |||||
void set1_challange4_test(); | |||||
void set1_challenge_5_test(); | |||||
void set1_challenge_6_test(); | |||||
namespace SET1 { | |||||
void run() | |||||
{ | |||||
set1_challenge3_test(); | |||||
set1_challange4_test(); | |||||
set1_challenge_5_test(); | |||||
set1_challenge_6_test(); | |||||
} | |||||
} | |||||
#endif// __set1_runner__ |
@@ -1,28 +1,8 @@ | |||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include "utils/hex_to_base64.h" | |||||
#include "utils/base64.h" | |||||
#include "set1/xor_char_finder.h" | #include "set1/xor_char_finder.h" | ||||
int char_in_array(const unsigned char iX, const char* const iArray, const int iSize ) | |||||
{ | |||||
int found = 0; | |||||
for(int i=0; i<iSize; ++i) | |||||
{ | |||||
if(iX==iArray[i]) | |||||
{ | |||||
found = 1; | |||||
break; | |||||
} | |||||
} | |||||
return found; | |||||
} | |||||
static void fill_frequency(struct frequency_t (&i_frequency_array)[FREQUENT_LETTERS_AMOUNT]) | |||||
{ | |||||
} | |||||
void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequency_t& o_frequency, unsigned ciphertext_len) | void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequency_t& o_frequency, unsigned ciphertext_len) | ||||
{ | { | ||||
@@ -68,13 +48,13 @@ void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequen | |||||
int fully_coppied=1; | int fully_coppied=1; | ||||
for(int j=0; j<ciphertext_len; ++j) | for(int j=0; j<ciphertext_len; ++j) | ||||
{ | { | ||||
const unsigned char X = p_ciphertext_xor[j]^aXorChar; | |||||
if( char_in_array(X, printable_ascii, printable_ascii_len) == 0) | |||||
const char xor_chr = p_ciphertext_xor[j]^aXorChar; | |||||
if( strchr(printable_ascii, xor_chr) == NULL) | |||||
{ | { | ||||
fully_coppied=0; | fully_coppied=0; | ||||
break; | break; | ||||
} | } | ||||
tmp[xorable][j] = X; | |||||
tmp[xorable][j] = xor_chr; | |||||
} | } | ||||
if(fully_coppied) | if(fully_coppied) | ||||
{ | { | ||||
@@ -1,7 +1,6 @@ | |||||
#define FREQUENT_LETTERS_AMOUNT 5 | #define FREQUENT_LETTERS_AMOUNT 5 | ||||
struct frequency_t { | struct frequency_t { | ||||
char letter; | char letter; | ||||
unsigned long frequency; | unsigned long frequency; | ||||
unsigned long score; | unsigned long score; | ||||
@@ -1,7 +1,8 @@ | |||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include "utils/hex_to_base64.h" | |||||
#include "utils/base64.h" | |||||
#include "utils/common.h" | |||||
#define MIN(a,b) a<b ? a : b | #define MIN(a,b) a<b ? a : b | ||||
@@ -1,11 +1,6 @@ | |||||
// data stores 24 bits | |||||
struct int24 { | |||||
unsigned int data:24; | |||||
}; | |||||
unsigned hex_to_base64(const char* hex_buff, char*& base64_buff, unsigned hex_buff_len ); | unsigned hex_to_base64(const char* hex_buff, char*& base64_buff, unsigned hex_buff_len ); | ||||
int base64_to_hex(const char* const i_string, int i_string_len, unsigned char*& o_hex_array ); | int base64_to_hex(const char* const i_string, int i_string_len, unsigned char*& o_hex_array ); | ||||
void convert_string_to_hex(const char* const iArray, const unsigned iArrayLen, unsigned char*& oHexArray); | void convert_string_to_hex(const char* const iArray, const unsigned iArrayLen, unsigned char*& oHexArray); | ||||
void convert_hex_to_string(const unsigned char* const iHexArray, unsigned iHexArrayLen, char*& oStringBuf ); | void convert_hex_to_string(const unsigned char* const iHexArray, unsigned iHexArrayLen, char*& oStringBuf ); | ||||
void xor_strings(const char* const iLeftString, const char* const iRightString, unsigned char*& oXorArray); | void xor_strings(const char* const iLeftString, const char* const iRightString, unsigned char*& oXorArray); | ||||
int c2b(char T); | |||||
int c2b(char T); |
@@ -0,0 +1,30 @@ | |||||
#include <stdlib.h> | |||||
#include <stdio.h> | |||||
#include <string.h> | |||||
unsigned long read_file_to_buffer(const char* const filepath, char*& obuff, int remove_new_line_chr) | |||||
{ | |||||
FILE* fh=0; | |||||
size_t len=0; | |||||
char* line=NULL; | |||||
unsigned long total_len=0; | |||||
if( (fh=fopen(filepath, "rb")) == 0 ) | |||||
{ | |||||
fprintf(stderr, "File %s doesn't exist. Exiting...\n", filepath); | |||||
exit(1); | |||||
} | |||||
while( getline(&line, &len, fh) != -1 ) | |||||
{ | |||||
len = strlen(line); | |||||
if(remove_new_line_chr) | |||||
len--; | |||||
strncpy(obuff+total_len, line, len); | |||||
total_len+=len; | |||||
free(line); | |||||
line=NULL; | |||||
} | |||||
return total_len; | |||||
} |
@@ -0,0 +1,3 @@ | |||||
struct int24 { unsigned int data:24; }; | |||||
unsigned long read_file_to_buffer(const char* const filepath, char*& obuff, int remove_new_line_chr = 1); |
@@ -1,6 +1,6 @@ | |||||
#include <string.h> | #include <string.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include "utils/hex_to_base64.h" | |||||
#include "utils/base64.h" | |||||
static unsigned calculate_nb_of_bits_set(unsigned char i_char) | static unsigned calculate_nb_of_bits_set(unsigned char i_char) | ||||
{ | { | ||||
@@ -0,0 +1,106 @@ | |||||
#include <string.h> | |||||
#include <assert.h> | |||||
#include <stdio.h> | |||||
#include <stdlib.h> | |||||
#include "utils/base64.h" | |||||
#include "utils/hamming.h" | |||||
#include "utils/xor.h" | |||||
#include "set1/xor_char_finder.h" | |||||
void hex_to_base64_test() { | |||||
const char test_buff1[] ="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"; | |||||
const char expected_buff1[] ="SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"; | |||||
const char test_buff2[] ="4927"; | |||||
const char expected_buff2[] ="SSH"; | |||||
const char test_buff3[] ="49"; | |||||
const char expected_buff3[] ="SB"; | |||||
const char test_buff4[] ="33"; | |||||
const char expected_buff4[] ="z"; | |||||
char output[256]; | |||||
char* po=output; | |||||
memset (output,'\0', 256); | |||||
hex_to_base64(test_buff1, po, strlen(test_buff1) ); | |||||
assert( memcmp(expected_buff1, output, strlen(expected_buff1)) == 0 ); | |||||
assert( strlen(expected_buff1) == strlen(output)); | |||||
// test case when there are 2 hex'es | |||||
memset (output,'\0', 256); | |||||
hex_to_base64(test_buff2, po, strlen(test_buff2) ); | |||||
assert( memcmp(expected_buff2, output, strlen(expected_buff2)) == 0 ); | |||||
assert( strlen(expected_buff2) == strlen(output)); | |||||
// test case when there is 1 hex | |||||
memset (output,'\0', 256); | |||||
hex_to_base64(test_buff3, po, strlen(test_buff3) ); | |||||
assert( memcmp(expected_buff3, output, strlen(expected_buff3)) == 0 ); | |||||
assert( strlen(expected_buff3) == strlen(output)); | |||||
// test case when there is 1 char | |||||
memset (output,'\0', 256); | |||||
hex_to_base64(test_buff4, po, strlen(test_buff4) ); | |||||
assert( memcmp(expected_buff4, output, strlen(expected_buff4)) == 0 ); | |||||
assert( strlen(expected_buff4) == strlen(output)); | |||||
} | |||||
void convert_string_to_hex_test() | |||||
{ | |||||
const char test_buff1[] = "49276d"; | |||||
unsigned char out_buff[3]; | |||||
unsigned char* p = out_buff; | |||||
convert_string_to_hex(test_buff1, strlen(test_buff1), p); | |||||
assert( p[0] == 0x49 | |||||
&& p[1] == 0x27 | |||||
&& p[2] == 0x6d); | |||||
// and back | |||||
char out_buf[6]; | |||||
char* op = out_buf; | |||||
convert_hex_to_string(p, 3, op); | |||||
assert( memcmp(op, test_buff1, 6) == 0 ); | |||||
} | |||||
void xor_strings_test() | |||||
{ | |||||
const char i_buf_1[] = "1c0111001f010100061a024b53535009181c"; | |||||
const char* p_ibuf1=i_buf_1; | |||||
const char i_buf_2[] = "686974207468652062756c6c277320657965"; | |||||
const char* p_ibuf2=i_buf_2; | |||||
char out_buf[36]; | |||||
char* p_out_buf=out_buf; | |||||
unsigned char buf[36/2]; | |||||
unsigned char* p_buf = buf; | |||||
xor_strings(p_ibuf1, p_ibuf2, p_buf); | |||||
convert_hex_to_string(buf, 36/2, p_out_buf); | |||||
assert( memcmp(out_buf, "746865206b696420646f6e277420706c6179", 36) == 0); | |||||
} | |||||
void hamming_test() | |||||
{ | |||||
static const char ch1[]="this is a test"; | |||||
static const char ch2[]="wokka wokka!!!"; | |||||
assert(block_distance(ch1,ch2,strlen(ch1)) == 37); | |||||
} | |||||
void base64_to_hex_test() | |||||
{ | |||||
static const char ch2[]="TWFuTWFu"; // 0x41 0x61 0x6E 0x41 0x61 0x6E | |||||
unsigned char out[6]; | |||||
unsigned char *p=out; | |||||
unsigned char expected[6] = {0x4D, 0x61, 0x6E, 0x4D, 0x61, 0x6E}; | |||||
base64_to_hex(ch2, 8, p); | |||||
assert( memcmp(expected, out, 6) == 0 ); | |||||
} | |||||
@@ -0,0 +1,22 @@ | |||||
#ifndef __utils_run__ | |||||
#define __utils_run__ | |||||
void hex_to_base64_test(); | |||||
unsigned long read_file_to_buffer(const char* const filepath, char*& obuff, int remove_new_line_chr = 1); | |||||
void xor_strings_test(); | |||||
void hamming_test(); | |||||
void base64_to_hex_test(); | |||||
namespace UTILS { | |||||
void run() { | |||||
hex_to_base64_test(); | |||||
// convert_string_to_hex_test(); | |||||
xor_strings_test(); | |||||
hamming_test(); | |||||
base64_to_hex_test(); | |||||
} | |||||
} | |||||
#endif // __utils_run__ |
@@ -1,270 +0,0 @@ | |||||
#include "hex_to_base64.h" | |||||
#include "set1/xor_char_finder.h" | |||||
#include "hamming.h" | |||||
#include <string.h> | |||||
#include <assert.h> | |||||
#include <stdio.h> | |||||
#include <stdlib.h> | |||||
#include "xor.h" | |||||
void hex_to_base64_text() { | |||||
const char test_buff1[] ="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"; | |||||
const char expected_buff1[] ="SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"; | |||||
const char test_buff2[] ="4927"; | |||||
const char expected_buff2[] ="SSH"; | |||||
const char test_buff3[] ="49"; | |||||
const char expected_buff3[] ="SB"; | |||||
const char test_buff4[] ="33"; | |||||
const char expected_buff4[] ="z"; | |||||
char output[256]; | |||||
char* po=output; | |||||
memset (output,'\0', 256); | |||||
hex_to_base64(test_buff1, po, strlen(test_buff1) ); | |||||
assert( memcmp(expected_buff1, output, strlen(expected_buff1)) == 0 ); | |||||
assert( strlen(expected_buff1) == strlen(output)); | |||||
// test case when there are 2 hex'es | |||||
memset (output,'\0', 256); | |||||
hex_to_base64(test_buff2, po, strlen(test_buff2) ); | |||||
assert( memcmp(expected_buff2, output, strlen(expected_buff2)) == 0 ); | |||||
assert( strlen(expected_buff2) == strlen(output)); | |||||
// test case when there is 1 hex | |||||
memset (output,'\0', 256); | |||||
hex_to_base64(test_buff3, po, strlen(test_buff3) ); | |||||
assert( memcmp(expected_buff3, output, strlen(expected_buff3)) == 0 ); | |||||
assert( strlen(expected_buff3) == strlen(output)); | |||||
// test case when there is 1 char | |||||
memset (output,'\0', 256); | |||||
hex_to_base64(test_buff4, po, strlen(test_buff4) ); | |||||
assert( memcmp(expected_buff4, output, strlen(expected_buff4)) == 0 ); | |||||
assert( strlen(expected_buff4) == strlen(output)); | |||||
} | |||||
unsigned long read_file_to_buffer(const char* const filepath, char*& obuff, int remove_new_line_chr = 1) | |||||
{ | |||||
FILE* fh=0; | |||||
size_t len=0; | |||||
char* line=NULL; | |||||
unsigned long total_len=0; | |||||
if( (fh=fopen(filepath, "rb")) == 0 ) | |||||
{ | |||||
fprintf(stderr, "File %s doesn't exist. Exiting...\n", filepath); | |||||
exit(1); | |||||
} | |||||
while( getline(&line, &len, fh) != -1 ) | |||||
{ | |||||
len = strlen(line); | |||||
if(remove_new_line_chr) | |||||
len--; | |||||
strncpy(obuff+total_len, line, len); | |||||
total_len+=len; | |||||
free(line); | |||||
line=NULL; | |||||
} | |||||
return total_len; | |||||
} | |||||
void convert_string_to_hex_test() | |||||
{ | |||||
const char test_buff1[] = "49276d"; | |||||
unsigned char out_buff[3]; | |||||
unsigned char* p = out_buff; | |||||
convert_string_to_hex(test_buff1, strlen(test_buff1), p); | |||||
assert( p[0] == 0x49 | |||||
&& p[1] == 0x27 | |||||
&& p[2] == 0x6d); | |||||
// and back | |||||
char out_buf[6]; | |||||
char* op = out_buf; | |||||
convert_hex_to_string(p, 3, op); | |||||
assert( memcmp(op, test_buff1, 6) == 0 ); | |||||
} | |||||
void xor_strings_test() | |||||
{ | |||||
const char i_buf_1[] = "1c0111001f010100061a024b53535009181c"; | |||||
const char* p_ibuf1=i_buf_1; | |||||
const char i_buf_2[] = "686974207468652062756c6c277320657965"; | |||||
const char* p_ibuf2=i_buf_2; | |||||
char out_buf[36]; | |||||
char* p_out_buf=out_buf; | |||||
unsigned char buf[36/2]; | |||||
unsigned char* p_buf = buf; | |||||
xor_strings(p_ibuf1, p_ibuf2, p_buf); | |||||
convert_hex_to_string(buf, 36/2, p_out_buf); | |||||
assert( memcmp(out_buf, "746865206b696420646f6e277420706c6179", 36) == 0); | |||||
} | |||||
void set1_challenge3_test() | |||||
{ | |||||
static const char ciphertext[] = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"; | |||||
struct frequency_t max_score; | |||||
// ----------------------------------------------------------------------------- | |||||
// | |||||
// 1. Convert ASCII ciphertext to HEX | |||||
// | |||||
// ----------------------------------------------------------------------------- | |||||
const int ciphertext_len = strlen(ciphertext) / 2; | |||||
unsigned char ciphertext_xor[ciphertext_len]; | |||||
unsigned char* p_ciphertext_xor = ciphertext_xor; | |||||
convert_string_to_hex(ciphertext, ciphertext_len*2, p_ciphertext_xor); | |||||
xor_char_finder(p_ciphertext_xor, max_score, ciphertext_len); | |||||
assert(max_score.letter == 'X'); | |||||
} | |||||
void set2_challange4_test() | |||||
{ | |||||
FILE* fp; | |||||
char* line=NULL; | |||||
size_t len=0; | |||||
int line_nb = 0; | |||||
unsigned char hexdump[30]; | |||||
unsigned char* p_hexdump = hexdump; | |||||
fp=fopen("etc/set1_t4_input.txt", "r"); | |||||
if(fp==NULL) | |||||
{ | |||||
fprintf(stderr, "etc/set1_t4_input.txt not found"); | |||||
return; | |||||
} | |||||
while( getline(&line, &len, fp) != -1 ) | |||||
{ | |||||
++line_nb; | |||||
// printf("%s",line); | |||||
struct frequency_t max_score; | |||||
max_score.letter=0x0; | |||||
max_score.score = 0; | |||||
// ----------------------------------------------------------------------------- | |||||
// | |||||
// 1. Convert ASCII ciphertext to HEX | |||||
// | |||||
// ----------------------------------------------------------------------------- | |||||
const int ciphertext_len = strlen(line) / 2; | |||||
unsigned char ciphertext_xor[ciphertext_len]; | |||||
unsigned char* p_ciphertext_xor = ciphertext_xor; | |||||
convert_string_to_hex(line, ciphertext_len*2, p_ciphertext_xor); | |||||
xor_char_finder(p_ciphertext_xor, max_score, ciphertext_len); | |||||
if(max_score.letter != 0x00) | |||||
{ | |||||
// printf("%d -> %c\n", line_nb, max_score.letter); | |||||
convert_string_to_hex(line, 60, p_hexdump); | |||||
for(int i=0; i<30;++i) | |||||
{ | |||||
// printf("%c", hexdump[i]^max_score.letter); | |||||
} | |||||
// printf("\n"); | |||||
} | |||||
free(line); line=0; len=0; | |||||
} | |||||
free(line); | |||||
} | |||||
void set2_challenge_5_test() | |||||
{ | |||||
static const char ch1[] = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"; | |||||
static const char xor_val[] = "ICE"; | |||||
static const unsigned char expected[] = { | |||||
0x0b,0x36,0x37,0x27,0x2a,0x2b,0x2e,0x63,0x62,0x2c,0x2e,0x69,0x69,0x2a,0x23,0x69, | |||||
0x3a,0x2a,0x3c,0x63,0x24,0x20,0x2d,0x62,0x3d,0x63,0x34,0x3c,0x2a,0x26,0x22,0x63, | |||||
0x24,0x27,0x27,0x65,0x27,0x2a,0x28,0x2b,0x2f,0x20,0x43,0x0a,0x65,0x2e,0x2c,0x65, | |||||
0x2a,0x31,0x24,0x33,0x3a,0x65,0x3e,0x2b,0x20,0x27,0x63,0x0c,0x69,0x2b,0x20,0x28, | |||||
0x31,0x65,0x28,0x63,0x26,0x30,0x2e,0x27,0x28,0x2f}; | |||||
unsigned char o_ch1[256]; | |||||
memset(o_ch1, '\0', 256); | |||||
unsigned char* po_ch1 = o_ch1; | |||||
xor_repeatedly(xor_val, ch1, po_ch1); | |||||
for(unsigned i=0; i<sizeof(expected)/sizeof(unsigned char); ++i) | |||||
assert( expected[i] == po_ch1[i] ); | |||||
} | |||||
void hamming_test() | |||||
{ | |||||
static const char ch1[]="this is a test"; | |||||
static const char ch2[]="wokka wokka!!!"; | |||||
assert(block_distance(ch1,ch2,strlen(ch1)) == 37); | |||||
} | |||||
void base64_to_hex_test() | |||||
{ | |||||
static const char ch2[]="TWFuTWFu"; // 0x41 0x61 0x6E 0x41 0x61 0x6E | |||||
unsigned char out[6]; | |||||
unsigned char *p=out; | |||||
unsigned char expected[6] = {0x4D, 0x61, 0x6E, 0x4D, 0x61, 0x6E}; | |||||
base64_to_hex(ch2, 8, p); | |||||
assert( memcmp(expected, out, 6) == 0 ); | |||||
} | |||||
void set2_challenge_6_test() | |||||
{ | |||||
char buf[1024*1024]; // 1 MB | |||||
unsigned char hex_buf[1024*1024]; | |||||
char* pbuf=buf; | |||||
unsigned char* p_hex_buf=hex_buf; | |||||
unsigned long len=read_file_to_buffer("etc/set1_t6.txt", pbuf); | |||||
buf[len]='\0'; | |||||
len=base64_to_hex(buf, len, p_hex_buf); | |||||
for(int i=0; i<len; ++i) | |||||
buf[i]=hex_buf[i]; | |||||
buf[len]='\0'; | |||||
// guess keysize | |||||
int keysize = choose_min_block_size(buf, strlen(buf), 2); | |||||
unsigned char* slide = new unsigned char[strlen(buf)/keysize]; | |||||
for(int j=0; j<keysize; ++j) | |||||
{ | |||||
memset(slide, '\0', strlen(buf)/keysize); | |||||
int counter = 0; | |||||
for(int i=0; i+j<strlen(buf); i+=keysize, counter++) | |||||
{ | |||||
slide[counter] = hex_buf[i+j]; | |||||
} | |||||
struct frequency_t max_score; | |||||
xor_char_finder(slide, max_score, counter); | |||||
printf("> %c %x\n", max_score.letter, max_score.letter); | |||||
} | |||||
// delete [] slide; | |||||
} | |||||
int main() { | |||||
hex_to_base64_text(); | |||||
convert_string_to_hex_test(); | |||||
xor_strings_test(); | |||||
//set1_challenge3_test(); | |||||
set2_challange4_test(); | |||||
set2_challenge_5_test(); | |||||
hamming_test(); | |||||
base64_to_hex_test(); | |||||
set2_challenge_6_test(); | |||||
} | |||||