Browse Source

Set1 fully working

api_change
Krzysztof KWIATKOWSKI 9 years ago
parent
commit
9b922022ee
4 changed files with 17 additions and 54 deletions
  1. +8
    -34
      set1/runner.cpp
  2. +4
    -8
      set1/runner.h
  3. +4
    -11
      set1/xor_char_finder.cpp
  4. +1
    -1
      set1/xor_char_finder.h

+ 8
- 34
set1/runner.cpp View File

@@ -137,33 +137,6 @@ void set1_challenge_5_test()
check( expected[i] == po_ch1[i] );
}


/*
I've a strong feeling that keylen is 29

i'm 'ack knD*i'm Ringin'eth
be%- *A rockSne on T-o mi$e s
Hileethe llY*GirlS yell OInee
cs= sY in th_ ack O#*me EWeh
L th$t's gyNj DeShay cu1ti+'
a%- Them Z'I HHittI+- ha=d eN
d t-e gixlIoS goIn' cra?y OVa
n -lA's on Nh' mikEi*manoI'in
otelazy$ *i'm Lettin'emyedru.
akIck in 0I6 conT7els "y iOut
heand C BoGin *To jus1 l t i=
afLow, leN /y coN&optsogo$*My
5osse-s~O thE side <el)in'ea
GO VanilVabGo! *OYmoo;h #Caus
tha~'S*The Way I w,llebe Cn
D if yoO &on't"cve . deMn, 1h
en WHsyoustarinb a1 meiKSO ge
t o
b'cauS *I c ntvOl t-e stk
gE**TheRe's noedi6sinnaaLlowe
d 0Iem in(s ow! plAse OThe mi
RfIes Sa y th y )ovei,eand th
*/
void set1_challenge_6_test()
{
const unsigned max_block_size=200;
@@ -182,17 +155,18 @@ void set1_challenge_6_test()

memset(out_buf,0,sizeof(out_buf));
int best_key_size = find_best_keysize(base64_buf, strlen(base64_buf));
best_key_size=decrypt_repeted_xor(base64_buf, out_buf, best_key_size);
check(best_key_size==29);
best_key_size=crack_repeted_xor(base64_buf, out_buf, best_key_size);
// sprintf((char*)error_buf, "Wrong keysize. Expected 5 got %d\n", best_key_size);
// check(memcmp(expected_plaintext1, out_buf, sizeof(expected_plaintext1))==0, out_buf);
printf("Key size: %d. Content:\n ", best_key_size);
for(int i=0; i<len; ++i)
printf("%.2x", out_buf[i]);
printf("%c", out_buf[i]);
//check(memcmp(expected_plaintext3, buf, sizeof(expected_plaintext3))==0, buf);

}

void decrypt_repeted_xor_test()
void crack_repeted_xor_test()
{

unsigned char error_buf[256];
@@ -217,7 +191,7 @@ void decrypt_repeted_xor_test()

memset(buf,0,sizeof(buf));
int best_key_size = find_best_keysize(hex_str1, strlen(hex_str1));
decrypt_repeted_xor(hex_str1, buf, best_key_size);
crack_repeted_xor(hex_str1, buf, best_key_size);
sprintf((char*)error_buf, "Wrong keysize. Expected 5 got %d\n", best_key_size);
check(best_key_size==5, error_buf);
check(memcmp(expected_plaintext1, buf, sizeof(expected_plaintext1))==0, buf);
@@ -225,7 +199,7 @@ void decrypt_repeted_xor_test()
// here I have to reduce key size otherwise with keylength 39 there is better score
memset(buf,0,sizeof(buf));
best_key_size = find_best_keysize(hex_str2, strlen(hex_str2),2,38);
decrypt_repeted_xor(hex_str2, buf, best_key_size);
crack_repeted_xor(hex_str2, buf, best_key_size);
sprintf((char*)error_buf, "Wrong keysize. Expected 13 got %d\n", best_key_size);
check(best_key_size==13, error_buf);
check(memcmp(expected_plaintext2, buf, sizeof(expected_plaintext2))==0, buf);
@@ -233,7 +207,7 @@ void decrypt_repeted_xor_test()
// here I have to reduce key size otherwise with keylength 39 there is better score
memset(buf,0,sizeof(buf));
best_key_size = find_best_keysize(hex_str3, strlen(hex_str3));
decrypt_repeted_xor(hex_str3, buf, best_key_size);
crack_repeted_xor(hex_str3, buf, best_key_size);
sprintf((char*)error_buf, "Wrong keysize. Expected 20 got %d\n", best_key_size);
check(best_key_size==20, error_buf);
check(memcmp(expected_plaintext3, buf, sizeof(expected_plaintext3))==0, buf);
@@ -241,7 +215,7 @@ void decrypt_repeted_xor_test()
/* this doesn't work
memset(buf,0,sizeof(buf));
best_key_size = find_best_keysize(hex_str4, strlen(hex_str4));
decrypt_repeted_xor(hex_str4, buf, best_key_size);
crack_repeted_xor(hex_str4, buf, best_key_size);
sprintf((char*)error_buf, "Wrong keysize. Expected 3 got %d\n", best_key_size);
check(best_key_size==3, error_buf);
check(memcmp(expected_plaintext4, buf, sizeof(expected_plaintext4))==0, buf);


+ 4
- 8
set1/runner.h View File

@@ -6,17 +6,17 @@ void set1_challange4_test();
void set1_challenge_5_test();
void set1_challenge_6_test();
void xor_char_finder_test();
void decrypt_repeted_xor_test();
void crack_repeted_xor_test();
void set1_challenge_7_test();
void set1_challenge_8_test();
namespace SET1 {

void run()
{
crack_repeted_xor_test();
//set1_challenge_6_test();
set1_challenge_7_test();
//decrypt_repeted_xor_test();

//
return;
xor_char_finder_test();
set1_challenge3_test();
set1_challange4_test();
@@ -29,7 +29,3 @@ namespace SET1 {
}

#endif// __set1_runner__

// Masz jakis tekst
// RybciaRybciaRybc
//

+ 4
- 11
set1/xor_char_finder.cpp View File

@@ -10,7 +10,8 @@

void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequency_t& o_frequency, unsigned ciphertext_len)
{
const char printable_ascii[] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!\"#$%&'()*+,-./:;<=>?@[\\]_{}|~`"; // '^' if I add this one then T3 doesn't work
const char printable_ascii[] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!\"#$%&'()*+,-./:;<=>?@[\\]_{}|~`^\r\n";
// const char printable_ascii[] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!()*+,-./:;<=>?@"; // '^' if I add this one then T3 doesn't work
const unsigned printable_ascii_len = sizeof(printable_ascii)/sizeof(char);


@@ -118,7 +119,7 @@ unsigned long long frequency_analysis(const unsigned char* i_text, const unsigne
return score;
}

int decrypt_repeted_xor(const char* const i_hex_string, unsigned char* o_buf, const unsigned i_min_block, const unsigned i_max_block)
int crack_repeted_xor(const char* const i_hex_string, unsigned char* o_buf, const unsigned i_min_block, const unsigned i_max_block)
{
unsigned len = strlen(i_hex_string)/2;
unsigned char* ciphertext_xor = new unsigned char[len];
@@ -190,7 +191,7 @@ int find_best_keysize(const char* i_hex_bytes, const unsigned i_len, const int i
while( current_keysize<i_max_keysize )
{
memset(buf, 0, i_len);
current_keysize = decrypt_repeted_xor(i_hex_bytes, buf, current_keysize, i_max_keysize);
current_keysize = crack_repeted_xor(i_hex_bytes, buf, current_keysize, i_max_keysize);

// check again if keysize is in the range. Otherwise range may not be respected.
if(current_keysize>i_max_keysize)
@@ -201,14 +202,6 @@ int find_best_keysize(const char* i_hex_bytes, const unsigned i_len, const int i
{
best_score = score;
block_size = current_keysize;
/*
printf("Score: %d keysize: %d\n", best_score, block_size);
for(int i=0; i<i_len; ++i)
{
printf("%c", buf[i]);
}
printf("\n");
*/
}
current_keysize++;
}


+ 1
- 1
set1/xor_char_finder.h View File

@@ -132,6 +132,6 @@ struct frequency_set {

double get_frequency(char c);
void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequency_t& o_frequency, unsigned ciphertext_len);
int decrypt_repeted_xor(const char* const i_hex_string, unsigned char* o_buf, const unsigned i_min_block = 3, const unsigned i_max_block = 40);
int crack_repeted_xor(const char* const i_hex_string, unsigned char* o_buf, const unsigned i_min_block = 3, const unsigned i_max_block = 40);
unsigned long long frequency_analysis(const unsigned char* i_text, const unsigned i_len);
int find_best_keysize(const char* i_hex_bytes, const unsigned i_len, const int i_min_keysize = 2, const int i_max_keysize = 40);

Loading…
Cancel
Save