Bläddra i källkod

Vanilla Ice is in the text

api_change
Krzysztof KWIATKOWSKI 9 år sedan
förälder
incheckning
bad3e44940
10 ändrade filer med 330 tillägg och 247 borttagningar
  1. +8
    -4
      Makefile
  2. +85
    -161
      MatsanoCrypto.sublime-workspace
  3. +3
    -1
      main.cpp
  4. +98
    -12
      set1/runner.cpp
  5. +11
    -6
      set1/runner.h
  6. +99
    -31
      set1/xor_char_finder.cpp
  7. +12
    -2
      set1/xor_char_finder.h
  8. +1
    -1
      utils/base64.cpp
  9. +10
    -26
      utils/hamming.c
  10. +3
    -3
      utils/runner.h

+ 8
- 4
Makefile Visa fil

@@ -1,16 +1,20 @@

INC=.
all:
mkdir -p obj
g++ -g -I${INC} -c utils/common.c -o obj/common.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 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 set1/runner.cpp -o obj/set1_runner.o
g++ -g -I${INC} -c main.cpp -o obj/main.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:
rm -rf obj
rm -rf obj

andrun: all
main

+ 85
- 161
MatsanoCrypto.sublime-workspace Visa fil

@@ -3,6 +3,10 @@
{
"selected_items":
[
[
"freq",
"frequency_analysis"
],
[
"base",
"base64_buf"
@@ -31,10 +35,6 @@
"tmp_",
"tmp_dist1"
],
[
"freq",
"frequencies"
],
[
"frequ",
"frequency_set"
@@ -523,31 +523,15 @@
"file": "set1/runner.cpp",
"settings":
{
"buffer_size": 9656,
"line_ending": "Unix"
}
},
{
"file": "utils/base64.cpp",
"settings":
{
"buffer_size": 8006,
"buffer_size": 10713,
"line_ending": "Unix"
}
},
{
"file": "utils/xor.c",
"file": "utils/hamming.c",
"settings":
{
"buffer_size": 374,
"line_ending": "Unix"
}
},
{
"file": "set1/xor_char_finder.cpp",
"settings":
{
"buffer_size": 4443,
"buffer_size": 1696,
"line_ending": "Unix"
}
},
@@ -555,31 +539,31 @@
"file": "set1/xor_char_finder.h",
"settings":
{
"buffer_size": 3017,
"buffer_size": 3136,
"line_ending": "Unix"
}
},
{
"file": "utils/runner.cpp",
"file": "set1/runner.h",
"settings":
{
"buffer_size": 4173,
"buffer_size": 490,
"line_ending": "Unix"
}
},
{
"file": "utils/common.h",
"file": "main.cpp",
"settings":
{
"buffer_size": 204,
"buffer_size": 101,
"line_ending": "Unix"
}
},
{
"file": "set1/runner.h",
"file": "utils/runner.h",
"settings":
{
"buffer_size": 479,
"buffer_size": 396,
"line_ending": "Unix"
}
},
@@ -600,17 +584,17 @@
"build_varint": "",
"command_palette":
{
"height": 312.0,
"last_filter": "toggle",
"height": 172.0,
"last_filter": "comment",
"selected_items":
[
[
"toggle",
"comment",
"Toggle Comment"
],
[
"comment",
"Snippet: comment-method-simple"
"toggle",
"Toggle Comment"
],
[
"commeen",
@@ -990,22 +974,22 @@
],
"file_history":
[
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/common.c",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/set1/xor_char_finder.cpp",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/base64.cpp",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/set1/xor_char_finder.h",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/set1/runner.h",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/common.h",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/hamming.c",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/runner.cpp",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/xor.c",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/common.c",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/MatsanoCrypto",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/common.h",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/set1/xor_char_finder.h",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/main.cpp",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/runner.h",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/set1/runner.h",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/runner.cpp",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/set1/runner.cpp",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/etc/set1_t6.txt.orig",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/etc/set1_t6.txt",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/hamming.h",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/hamming.c",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/base64.h",
"/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/xor.h",
"/home/kkwiatkowski/t",
@@ -1194,6 +1178,15 @@
"case_sensitive": false,
"find_history":
[
"printf",
"DIST",
"frequency",
"FREQUENT_LETTERS_AMOUNT",
"// ",
"frequency_set",
"decrypt_repeted_xor_test",
"decrypt_repeted_xor",
"choose_min_block_size",
"convert_hex",
"choose_min\nchoose_min\nchoose_min",
"choose_min",
@@ -1312,16 +1305,7 @@
"TransportProtocolHandlerBase",
"reinterpret_cast",
"EDH",
"ECDH",
"EDH",
"\"EDH\"",
"\"STRONG\"",
"STRONG",
"EDHC",
"DEBUG",
"TRC1ASIEDI",
"IN{cfg.srv[0].name}-TCIL",
"socket"
"ECDH"
],
"highlight": true,
"in_selection": false,
@@ -1384,7 +1368,7 @@
"groups":
[
{
"selected": 5,
"selected": 0,
"sheets":
[
{
@@ -1393,228 +1377,168 @@
"semi_transient": false,
"settings":
{
"buffer_size": 9656,
"buffer_size": 10713,
"regions":
{
},
"selection":
[
[
9184,
9184
4564,
4564
]
],
"settings":
{
"function_name_status_row": 170,
"function_name_status_row": 134,
"syntax": "Packages/C++/C++.tmLanguage",
"translate_tabs_to_spaces": false
},
"translation.x": 0.0,
"translation.y": 1627.0,
"translation.y": 1319.0,
"zoom_level": 1.0
},
"stack_index": 1,
"stack_index": 0,
"type": "text"
},
{
"buffer": 1,
"file": "utils/base64.cpp",
"file": "utils/hamming.c",
"semi_transient": false,
"settings":
{
"buffer_size": 8006,
"buffer_size": 1696,
"regions":
{
},
"selection":
[
[
8006,
8006
1614,
1614
]
],
"settings":
{
"function_name_status_row": 258,
"syntax": "Packages/C++/C++.tmLanguage",
"function_name_status_row": 55,
"syntax": "Packages/C Improved/C Improved.tmLanguage",
"translate_tabs_to_spaces": false
},
"translation.x": 0.0,
"translation.y": 2689.0,
"zoom_level": 1.0
},
"stack_index": 2,
"type": "text"
},
{
"buffer": 2,
"file": "utils/xor.c",
"semi_transient": false,
"settings":
{
"buffer_size": 374,
"regions":
{
},
"selection":
[
[
374,
374
]
],
"settings":
{
"function_name_status_row": 13,
"syntax": "Packages/C Improved/C Improved.tmLanguage"
},
"translation.x": 0.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 4,
"type": "text"
},
{
"buffer": 3,
"file": "set1/xor_char_finder.cpp",
"semi_transient": true,
"settings":
{
"buffer_size": 4443,
"regions":
{
},
"selection":
[
[
4443,
4443
]
],
"settings":
{
"function_name_status_row": 146,
"syntax": "Packages/C++/C++.tmLanguage",
"translate_tabs_to_spaces": false
},
"translation.x": -0.0,
"translation.y": 1121.0,
"zoom_level": 1.0
},
"stack_index": 3,
"stack_index": 1,
"type": "text"
},
{
"buffer": 4,
"buffer": 2,
"file": "set1/xor_char_finder.h",
"semi_transient": false,
"settings":
{
"buffer_size": 3017,
"buffer_size": 3136,
"regions":
{
},
"selection":
[
[
3013,
3013
2938,
2938
]
],
"settings":
{
"function_name_status_row": 126,
"function_name_status_row": 128,
"syntax": "Packages/C Improved/C Improved.tmLanguage",
"translate_tabs_to_spaces": false
},
"translation.x": 0.0,
"translation.y": 855.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 6,
"stack_index": 3,
"type": "text"
},
{
"buffer": 5,
"file": "utils/runner.cpp",
"buffer": 3,
"file": "set1/runner.h",
"semi_transient": false,
"settings":
{
"buffer_size": 4173,
"buffer_size": 490,
"regions":
{
},
"selection":
[
[
3260,
3260
287,
287
]
],
"settings":
{
"function_name_status_row": 106,
"syntax": "Packages/C++/C++.tmLanguage",
"translate_tabs_to_spaces": false
"function_name_status_row": 14,
"syntax": "Packages/C Improved/C Improved.tmLanguage"
},
"translation.x": 0.0,
"translation.y": 910.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 0,
"stack_index": 2,
"type": "text"
},
{
"buffer": 6,
"file": "utils/common.h",
"buffer": 4,
"file": "main.cpp",
"semi_transient": false,
"settings":
{
"buffer_size": 204,
"buffer_size": 101,
"regions":
{
},
"selection":
[
[
56,
56
72,
72
]
],
"settings":
{
"function_name_status_row": 0,
"syntax": "Packages/C Improved/C Improved.tmLanguage"
"function_name_status_row": 5,
"syntax": "Packages/C++/C++.tmLanguage"
},
"translation.x": 0.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 7,
"stack_index": 4,
"type": "text"
},
{
"buffer": 7,
"file": "set1/runner.h",
"buffer": 5,
"file": "utils/runner.h",
"semi_transient": false,
"settings":
{
"buffer_size": 479,
"buffer_size": 396,
"regions":
{
},
"selection":
[
[
274,
274
0,
0
]
],
"settings":
{
"function_name_status_row": 14,
"function_name_status_row": 0,
"syntax": "Packages/C Improved/C Improved.tmLanguage"
},
"translation.x": 0.0,
@@ -1625,7 +1549,7 @@
"type": "text"
},
{
"buffer": 8,
"buffer": 6,
"semi_transient": false,
"settings":
{
@@ -1651,7 +1575,7 @@
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 8,
"stack_index": 6,
"type": "text"
}
]
@@ -1729,6 +1653,10 @@
"last_filter": "",
"selected_items":
[
[
"hamming.c",
"utils/hamming.c"
],
[
"common.h",
"utils/common.h"
@@ -2236,10 +2164,6 @@
[
"cmdnewsetdelbasepeakversion.h",
"SI/masteragent/magadm/CmdNewSetDelBasePeakVersion.h"
],
[
"cmdnewsetdelbasepeakversionte",
"SI/masteragent/test/src/CmdNewSetDelBasePeakVersionTest.cpp"
]
],
"width": 0.0


+ 3
- 1
main.cpp Visa fil

@@ -3,6 +3,8 @@

int main()
{
// UTILS::run();
// UTILS::run();
SET1::run();

return 0;
}

+ 98
- 12
set1/runner.cpp Visa fil

@@ -25,8 +25,13 @@ char guess_encryption_key_letter(const char ciphertext_hex[])
void set1_challenge3_test()
{
static const char ciphertext[] = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736";
guess_encryption_key_letter(ciphertext);
check(guess_encryption_key_letter(ciphertext) == 'X');
unsigned char buf[2];
char ret;

ret = guess_encryption_key_letter(ciphertext);

sprintf((char*)buf, "%c", ret);
check(ret == 'X', buf);
}


@@ -132,24 +137,65 @@ 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;

char base64_buf[1024*1024]; // 1 MB
char error_buf[256];
unsigned char out_buf[1024*1024];
unsigned char hex_buf[1024*1024];
unsigned long len=read_file_to_buffer("etc/set1_t6.txt", base64_buf);
base64_buf[len]='\0';
len=base64_to_hex(base64_buf, len, hex_buf);

convert_hex_to_string(hex_buf, len, base64_buf);
printf("%s\n", base64_buf);
decrypt_repeted_xor(base64_buf, out_buf);

// now base64_buf contains string with ciphertext that is HEX-encoded

memset(out_buf,0,sizeof(out_buf));
int best_key_size = find_best_keysize(base64_buf, strlen(base64_buf),29,30);
decrypt_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);
for(int i=0; i<len; ++i)
printf("%c", out_buf[i]);
//check(memcmp(expected_plaintext3, buf, sizeof(expected_plaintext3))==0, buf);

}

void decrypt_repeted_xor_test()
{
unsigned char expected_plaintext1[] = "Big bang theory: Ok, go home crazy men. Very well, I can't keep up with this secret anylonger. Ok, you listen to me but it's not any of your bussiness. Got it? Worst bed time story every. Regardless of your feelings I would like to break with this guy.";

unsigned char error_buf[256];
unsigned char expected_plaintext1[] = "Big bang theory: Ok, go home crazy men. Very well, I can't keep up with this secret anylonger. Ok, you listen to me but it's not any of your bussiness. Got it? Worst bed time story every. Regardless of your feelings I would like to break with this guy.";
unsigned char expected_plaintext2[] = "This is an english text that is going to be encrypted with multiple characters. Then I need to write algorithm that will decrypt it. The way to decrypt it is to first calculate hamming distance in order to guess length of the key. This may fail, but maybe will work. So then I'll decrypt it in some fancy way and that's it. It will work, I'm sure about it. OK, now I'm done with this stupid text. Second episode of Big Bang theory is starting.";
unsigned char expected_plaintext3[] = "Big bang theory: Ok, go home crazy men. Very well, I can't keep up with this secret anylonger. Ok, you listen to me but it's not any of your bussiness. Got it? Worst bed time story every. Regardless of your feelings I would like to break with this guy.";
unsigned char expected_plaintext4[] = "Big bang theory: Ok, go home crazy men. Very well, I can't keep up with this secret anylonger. Ok, you listen to me but it's not any of your bussiness. Got it? Worst bed time story every. Regardless of your feelings I would like to break with this guy.";
@@ -167,16 +213,56 @@ void decrypt_repeted_xor_test()
char hex_str4[] = "360002540b041a0e450001001b1b1c4e492a1f45451306451c060811490606081f0d490811074b543f00061045030c091845453d490615074200490e110c15541c15541e0c00014500010c074916110a17111d4515071c18060b130c175a492a1f45450d061054050c071d001a49111b4908114907011d451d1d4207490b1b1d4515071c54060354100a011b45161c1607000b111a165a49221b1d451d1d5a543e0a061a11540b001049111d0400541a111b1b1c540c13111b1c5a4937110e04060d09111a1654060354100a011b45120c0018000b131a453d49121b1c091049091d0200541d0a540b1711080e541e0c00014500010c07490201104b";

unsigned char buf[1024*1024];
decrypt_repeted_xor(hex_str1, buf);

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);
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);

decrypt_repeted_xor(hex_str2, buf);
// 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);
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);

// decrypt_repeted_xor(hex_str3, buf, 4);
// memset(buf, 0, 1024*1024);
//check(memcmp(expected_plaintext3, buf, sizeof(expected_plaintext3))==0, buf);
// 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);
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);

/* 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);
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);
*/
}

void testing()
{

unsigned char buf[1024*1024];

memset(buf,0,sizeof(buf));
unsigned char error_buf[256];
unsigned char expected_plaintext1[] = "Big bang theory: Ok, go home crazy men. Very well, I can't keep up with this secret anylonger. Ok, you listen to me but it's not any of your bussiness. Got it? Worst bed time story every. Regardless of your feelings I would like to break with this guy.";
char hex_str4[] = "3101085216531d0f4f061c571c1a1648547d18444f151b121b07021754510109150b545f16064152225701114f05115e1f444f3b5451120648065459160d1f520142531f06061c12070006015441160b1d1700121206161e1b5c140d1d5c547d18444f0b1b475304060100571d481b1d545f16480d0700121a1c4801545c1c1c4f131a4b530709520d5d061a4f1001410001011707415d48281d00121a1c5052235d011b1b52165717481b1b1957531b1b1d064b530d1917064b5d483d171353010c03170741530709520d5d061a4f1411571f01011507123a48181d015e1748031b1f57531c0052164016090452035b07004f061c5b004808070d1c ";

memset(buf,0,sizeof(buf));
int best_key_size = find_best_keysize(hex_str4, strlen(hex_str4),2,10);
decrypt_repeted_xor(hex_str4, buf, best_key_size);

// decrypt_repeted_xor(hex_str4, buf);
// check(memcmp(expected_plaintext4, buf, sizeof(expected_plaintext4))==0, buf);
for(int i=0; i<strlen(hex_str4)/2; i++)
{
printf("%c", buf[i]);
}
}

+ 11
- 6
set1/runner.h Visa fil

@@ -12,15 +12,20 @@ namespace SET1 {

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

//decrypt_repeted_xor_test();
return; xor_char_finder_test();
set1_challenge3_test();
set1_challange4_test();
set1_challenge_5_test();

// xor_char_finder_test();
// set1_challenge3_test();
// set1_challange4_test();
// set1_challenge_5_test();
set1_challenge_6_test();
}

}

#endif// __set1_runner__

// Masz jakis tekst
// RybciaRybciaRybc
//

+ 99
- 31
set1/xor_char_finder.cpp Visa fil

@@ -5,21 +5,14 @@
#include "utils/hamming.h"
#include "utils/xor.h"
#include "set1/xor_char_finder.h"
#include <ctype.h>
#include <stdlib.h>

void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequency_t& o_frequency, unsigned ciphertext_len)
{
const char printable_ascii[] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_{}|~`";
//const char printable_ascii[] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
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);

// -----------------------------------------------------------------------------
//
// Array keeps letters that occure most frequently in english language. They are
// ordered from most frequent to least frequent
//
// -----------------------------------------------------------------------------
frequency_set frequency;


// -----------------------------------------------------------------------------
//
@@ -28,7 +21,7 @@ void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequen
//
// -----------------------------------------------------------------------------
int xorable = 0;
char xorable_chars[printable_ascii_len];
unsigned char xorable_chars[printable_ascii_len];


// -----------------------------------------------------------------------------
@@ -60,35 +53,36 @@ void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequen
}
// -----------------------------------------------------------------------------
//
// 3. Calculate best score - get this text from xorables that has most occurences
// of letters from frequency list.
// 3. Frequency analysis: select xorable that xors to most probable result
//
// -----------------------------------------------------------------------------
o_frequency.frequency=0;
o_frequency.letter=0x00; // > is not in the set
o_frequency.score = 0;
double fq = 0;

unsigned char* plaintext = new unsigned char[ciphertext_len];
unsigned char ch[1];
if( xorable > 1 )
{
for(int i=0; i<xorable; ++i)
{
unsigned long long score=0;
for(int j=0; j<ciphertext_len; ++j)
xor_repeatedly(&xorable_chars[i], 1, p_ciphertext_xor, ciphertext_len, plaintext);
unsigned long long score = frequency_analysis(plaintext, ciphertext_len);

if(o_frequency.score > 0 && (score == o_frequency.score) )
{
for(int k=0; k<FREQUENT_LETTERS_AMOUNT; ++k)
if(tolower(o_frequency.letter) == tolower(xorable_chars[i]))
{
if(frequency[k].letter == (p_ciphertext_xor[j]^xorable_chars[i]))
{
unsigned long long tmp = score + frequency[k].frequency;
// crash if overflow
assert(tmp>score);
score =tmp;
}
// in case two same letters are found always prefer small one (more probable)
o_frequency.letter = tolower(xorable_chars[i]);
}
else
{
printf("Two results with equal scores.\n");
delete [] plaintext;
return;
}
}
if(o_frequency.score > 0 && (score == o_frequency.score) )
{
printf("Two results with equal scores. Extend number of frequent letters\n");
return;
}
else if(score > o_frequency.score)
{
@@ -103,10 +97,28 @@ void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequen
o_frequency.frequency=0;
o_frequency.score = 0;
}
delete [] plaintext;
}

unsigned long long frequency_analysis(const unsigned char* i_text, const unsigned i_len)
{
unsigned long long score=0;
double fq = 0;
for(int j=0; j<i_len; ++j)
{
unsigned char ch = tolower(i_text[j]);
if( (fq = get_frequency(ch)) )
{
unsigned long long tmp = score + fq;
// crash if overflow
assert(tmp>score);
score =tmp;
}
}
return score;
}

void decrypt_repeted_xor(const char* const i_hex_string, unsigned char* o_buf, const unsigned i_min_block )
int decrypt_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];
@@ -114,7 +126,7 @@ void decrypt_repeted_xor(const char* const i_hex_string, unsigned char* o_buf, c
convert_string_to_hex(i_hex_string, strlen(i_hex_string), ciphertext_xor);

unsigned char found_key[120];
int keysize = choose_min_block_size(ciphertext_xor, len, i_min_block, 40);
int keysize = choose_min_block_size(ciphertext_xor, len, i_min_block, i_max_block);
fprintf(stdout, "Key Size: %d Hex Len: %d, Min key size: %d, SlideNb: %d\n",
keysize,
len,
@@ -136,11 +148,67 @@ void decrypt_repeted_xor(const char* const i_hex_string, unsigned char* o_buf, c
struct frequency_t max_score;
xor_char_finder(slide, max_score, block_nb);

printf("> Score %d %c\n", max_score.score, max_score.letter);
// printf("> Score %d %c\n", max_score.score, max_score.letter);
found_key[j]=max_score.letter;
}
xor_repeatedly(found_key, keysize, ciphertext_xor, len, o_buf);

delete [] slide;
delete [] ciphertext_xor;

return keysize;
}

double get_frequency(char c)
{
// -----------------------------------------------------------------------------
//
// Array keeps letters that occure most frequently in english language. They are
// ordered from most frequent to least frequent
//
// -----------------------------------------------------------------------------
frequency_set frequency;

for(int k=0; k<FREQUENT_LETTERS_AMOUNT; ++k)
{
if(frequency[k].letter == tolower(c))
{
return frequency[k].frequency;
}
}
return 0;
}

int find_best_keysize(const char* i_hex_bytes, const unsigned i_len, const int i_min_keysize, const int i_max_keysize)
{
int score, best_score, block_size;
score=best_score=block_size=0;
int current_keysize = i_min_keysize;

unsigned char* buf = (unsigned char*) malloc(i_len);
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);

// check again if keysize is in the range. Otherwise range may not be respected.
if(current_keysize>i_max_keysize)
break;

score=frequency_analysis(buf, i_len);
if(score>best_score)
{
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++;
}
free(buf);
return block_size;
}

+ 12
- 2
set1/xor_char_finder.h Visa fil

@@ -1,4 +1,4 @@
#define FREQUENT_LETTERS_AMOUNT 26
#define FREQUENT_LETTERS_AMOUNT 27

struct frequency_t {
unsigned char letter;
@@ -117,11 +117,21 @@ struct frequency_set {
frequencies[25].letter='z';
frequencies[25].frequency=74;
frequencies[25].score=0;

// everything that's not alphabet

frequencies[26].letter=' ';
frequencies[26].frequency=12703; // 'e' + 1
frequencies[26].score=0;
}
const frequency_t& operator[](const unsigned idx) {
return frequencies[idx];
}
};


double get_frequency(char c);
void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequency_t& o_frequency, unsigned ciphertext_len);
void decrypt_repeted_xor(const char* const i_hex_string, unsigned char* o_buf, const unsigned i_min_block = 2 );
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);
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);

+ 1
- 1
utils/base64.cpp Visa fil

@@ -91,7 +91,7 @@ void convert_hex_to_string(const unsigned char* const iHexArray, unsigned iHexAr
{
for(unsigned i=0; i<iHexArrayLen; i++)
{
sprintf(oStringBuf+(i*2), "%x", iHexArray[i]);
sprintf(oStringBuf+(i*2), "%.2x", iHexArray[i]);
}
}



+ 10
- 26
utils/hamming.c Visa fil

@@ -13,7 +13,7 @@ int block_distance(const unsigned char* const s1, const unsigned char* const s2,
for(counter=0; counter<i_size; ++counter)
{
// calc nb bits set
unsigned char ch = s1[counter]^s2[counter];
unsigned char ch = s1[counter]^s2[counter];
while(ch!=0)
{
dist++;
@@ -25,13 +25,13 @@ int block_distance(const unsigned char* const s1, const unsigned char* const s2,
return dist;
}

int choose_min_block_size(const unsigned char* const i_string,
const unsigned long i_size,
const unsigned i_min_block_size,
int choose_min_block_size(const unsigned char* const i_string,
const unsigned long i_size,
const unsigned i_min_block_size,
const unsigned i_max_block_size)
{
unsigned block_size = 0;
double min_dist=i_max_block_size*8;
double min_dist=i_max_block_size*8;
int min_block_size = i_max_block_size;

// temporary results
@@ -43,33 +43,17 @@ int choose_min_block_size(const unsigned char* const i_string,
// take 4 blocks, calc distance, normalize and get min

tmp_dist1 = block_distance(i_string, i_string+block_size, block_size);
tmp_dist1 = tmp_dist1/block_size;
tmp_dist1 = tmp_dist1/(block_size*8);
tmp_dist2 = block_distance(i_string+(block_size*2), i_string+(block_size*3), block_size);
tmp_dist2 = tmp_dist2/block_size;
/*
tmp_dist1 = tmp_dist1/block_size;

//tmp_dist3 = block_distance(i_string+(block_size*4), i_string+(block_size*5), block_size);
//tmp_dist3 = tmp_dist3/block_size;
//printf("DIST %d %f %f %f\n", block_size, tmp_dist1, tmp_dist2, tmp_dist3);

// get smallest average
//tmp_dist1 = (tmp_dist1+tmp_dist2+tmp_dist3 ) / 3;

// take smaller of tmp_dist1/2 and compare to total minimum

if(tmp_dist1<min_dist) {
min_dist = tmp_dist1;
min_block_size = block_size;
}*/
tmp_dist2 = tmp_dist2/(block_size*8);

if(tmp_dist2<tmp_dist1)
tmp_dist1 = tmp_dist2;
tmp_dist1 = tmp_dist2;
if(tmp_dist1<min_dist) {
min_dist = tmp_dist1;
min_block_size = block_size;
}
printf("DIST: %f BS: %d\n", tmp_dist1, block_size);
}
// printf("DIST: %f BS: %d\n", tmp_dist1, block_size);

}
return min_block_size;


+ 3
- 3
utils/runner.h Visa fil

@@ -9,9 +9,9 @@ void convert_string_to_hex_test();
void c2b_test();

namespace UTILS {
void run() {
hamming_test();
void run() {
hamming_test();
hex_to_base64_test();
convert_string_to_hex_test();
xor_strings_test();


Laddar…
Avbryt
Spara