From bad3e44940f1997ee649094b1a188416554080d5 Mon Sep 17 00:00:00 2001 From: Krzysztof KWIATKOWSKI Date: Fri, 12 Jun 2015 00:42:18 +0200 Subject: [PATCH] Vanilla Ice is in the text --- Makefile | 12 +- MatsanoCrypto.sublime-workspace | 246 +++++++++++--------------------- main.cpp | 4 +- set1/runner.cpp | 110 ++++++++++++-- set1/runner.h | 17 ++- set1/xor_char_finder.cpp | 130 +++++++++++++---- set1/xor_char_finder.h | 14 +- utils/base64.cpp | 2 +- utils/hamming.c | 36 ++--- utils/runner.h | 6 +- 10 files changed, 330 insertions(+), 247 deletions(-) diff --git a/Makefile b/Makefile index 1a65296..6c2177e 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/MatsanoCrypto.sublime-workspace b/MatsanoCrypto.sublime-workspace index 91ab1b7..e0877d6 100644 --- a/MatsanoCrypto.sublime-workspace +++ b/MatsanoCrypto.sublime-workspace @@ -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 diff --git a/main.cpp b/main.cpp index 5430abf..f6cecf7 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,8 @@ int main() { - // UTILS::run(); + // UTILS::run(); SET1::run(); + + return 0; } \ No newline at end of file diff --git a/set1/runner.cpp b/set1/runner.cpp index 19aea8c..b9d9d70 100644 --- a/set1/runner.cpp +++ b/set1/runner.cpp @@ -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 +#include 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 0 && (score == o_frequency.score) ) { - for(int k=0; kscore); - 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; jscore); + 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; ki_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