From f835c605b8f80da33e7cdeb2d099f9ea037f806f Mon Sep 17 00:00:00 2001 From: Krzysztof KWIATKOWSKI Date: Fri, 5 Jun 2015 09:54:52 +0200 Subject: [PATCH] T6 not working yet --- MatsanoCrypto | 44 ++- MatsanoCrypto.sublime-workspace | 499 ++++++++++++++++++-------------- etc/set1_t6.txt | 51 +--- etc/set1_t6.txt.orig | 64 ++++ main.cpp | 2 +- set1/runner.cpp | 157 ++++++---- set1/runner.h | 15 +- set1/xor_char_finder.cpp | 88 ++++-- set1/xor_char_finder.h | 103 ++++++- utils/base64.cpp | 104 ++++--- utils/base64.h | 10 +- utils/common.c | 11 +- utils/common.h | 4 +- utils/hamming.c | 70 +++-- utils/hamming.h | 7 +- utils/runner.cpp | 125 +++++--- utils/runner.h | 10 +- utils/xor.c | 11 +- utils/xor.h | 2 +- 19 files changed, 878 insertions(+), 499 deletions(-) create mode 100644 etc/set1_t6.txt.orig diff --git a/MatsanoCrypto b/MatsanoCrypto index 24db303..af011f2 100644 --- a/MatsanoCrypto +++ b/MatsanoCrypto @@ -4,5 +4,47 @@ { "path": "." } - ] + ], + "settings": + { + "rsync_ssh": + { + "excludes": + [ + ".git*", + "_build", + "blib", + "Build" + ], + "options": + [ + "--dry-run", + "--delete" + ], + "remotes": + { + "MatasanoCrypto": + [ + { + "enabled": 1, + "excludes": + [ + ], + "options": + [ + ], + "remote_host": "my-server.my-domain.tld", + "remote_path": "/home/kkwiatkowski/Projects/MatasanoCrypto", + "remote_port": 22, + "remote_post_command": "", + "remote_pre_command": "", + "remote_user": "kkwiatkowski" + } + ] + }, + "sync_on_save": true + }, + "translate_tabs_to_spaces": true, + "trim_trailing_white_space_on_save": true + } } diff --git a/MatsanoCrypto.sublime-workspace b/MatsanoCrypto.sublime-workspace index c0ed37a..91ab1b7 100644 --- a/MatsanoCrypto.sublime-workspace +++ b/MatsanoCrypto.sublime-workspace @@ -4,24 +4,44 @@ "selected_items": [ [ - "freq", - "frequencies" + "base", + "base64_buf" ], [ - "frequ", - "frequency_set" + "expe", + "expected_plaintext" ], [ - "min", - "min_dist" + "ciph", + "ciphertext_5" + ], + [ + "hex_", + "hex_str_buf" + ], + [ + "tmp_dis", + "tmp_dist3" ], [ "tmp_di", - "tmp_dist1" + "tmp_dist2" ], [ "tmp_", - "tmp_dist2" + "tmp_dist1" + ], + [ + "freq", + "frequencies" + ], + [ + "frequ", + "frequency_set" + ], + [ + "min", + "min_dist" ], [ "block", @@ -494,60 +514,40 @@ [ "subs", "subscribe" - ], - [ - "onRece", - "onInitReceived" - ], - [ - "subsc", - "subscriber" - ], - [ - "Subsc", - "Subscribers" - ], - [ - "kSEI", - "kSEIErr_ImplementationError" - ], - [ - "ConfigEve", - "ConfigEventSubscriberInterfaceWeakPtr" ] ] }, "buffers": [ { - "file": "utils/hamming.c", + "file": "set1/runner.cpp", "settings": { - "buffer_size": 1465, + "buffer_size": 9656, "line_ending": "Unix" } }, { - "file": "utils/hamming.h", + "file": "utils/base64.cpp", "settings": { - "buffer_size": 190, + "buffer_size": 8006, "line_ending": "Unix" } }, { - "file": "set1/xor_char_finder.cpp", + "file": "utils/xor.c", "settings": { - "buffer_size": 3565, + "buffer_size": 374, "line_ending": "Unix" } }, { - "file": "utils/xor.h", + "file": "set1/xor_char_finder.cpp", "settings": { - "buffer_size": 98, + "buffer_size": 4443, "line_ending": "Unix" } }, @@ -555,25 +555,42 @@ "file": "set1/xor_char_finder.h", "settings": { - "buffer_size": 973, + "buffer_size": 3017, "line_ending": "Unix" } }, { - "file": "utils/hex_to_base64.cpp", + "file": "utils/runner.cpp", "settings": { - "buffer_size": 7716, + "buffer_size": 4173, "line_ending": "Unix" } }, { - "file": "utils/utils_tester.cpp", + "file": "utils/common.h", "settings": { - "buffer_size": 7345, + "buffer_size": 204, "line_ending": "Unix" } + }, + { + "file": "set1/runner.h", + "settings": + { + "buffer_size": 479, + "line_ending": "Unix" + } + }, + { + "contents": "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.", + "settings": + { + "buffer_size": 443, + "line_ending": "Unix", + "name": "This is an english text that is going to be encryp" + } } ], "build_system": "", @@ -583,10 +600,22 @@ "build_varint": "", "command_palette": { - "height": 52.0, - "last_filter": "", + "height": 312.0, + "last_filter": "toggle", "selected_items": [ + [ + "toggle", + "Toggle Comment" + ], + [ + "comment", + "Snippet: comment-method-simple" + ], + [ + "commeen", + "Snippet: comment-method" + ], [ "comme", "Snippet: comment-method" @@ -595,10 +624,6 @@ "install", "Package Control: Install Package" ], - [ - "comment", - "Snippet: comment" - ], [ "snippet", "Snippet: comment" @@ -695,10 +720,6 @@ "markd", "Markdown Preview: Preview in Browser" ], - [ - "toggle", - "Toggle Comment" - ], [ "python", "Set Syntax: Python" @@ -969,11 +990,30 @@ ], "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/utils/xor.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/utils/hex_to_base64.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", + "/home/kkwiatkowski/xorer.py", + "/home/kkwiatkowski/t1.cpp", "/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/hex_to_base64.cpp", "/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/utils_tester.cpp", + "/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/hex_to_base64.h", "/home/flowher/test.md", "/home/flowher/test.c", "/home/flowher/repos/MatasanoCrypto/etc/set1_t6.txt", @@ -988,7 +1028,6 @@ "/home/flowher/repos/MatasanoCrypto/utils/utils_tester.cpp", "/home/flowher/repos/MatasanoCrypto/utils/xor.h", "/home/flowher/repos/MatasanoCrypto/utils/xor.c", - "/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils/xor.c", "/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/set1/challenge3.h", "/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/set1/challenge3.cpp", "/home/kkwiatkowski/.sublime", @@ -1078,25 +1117,7 @@ "/home/kkwiatkowski/amadeus_workdir/repos/02_SI/br_5-1/agent/src/Agent.cpp", "/home/kkwiatkowski/amadeus_workdir/repos/03_Components/ACF/br_12-0-0/src/network/TCPAcceptor.cpp", "/home/kkwiatkowski/amadeus_workdir/repos/02_SI/br_5-1/config/include/si/model/TableClientProfile.h", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/crypto/srp/srp_lib.c", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/crypto/bn/bn.h", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/crypto/bn/bn_exp.c", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/crypto/srp/srp_lcl.h", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/apps/srp.c", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/doc/crypto/SMIME_read_PKCS7.pod", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/apps/s512-req.pem", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/Netware/set_env.bat", - "/home/kkwiatkowski/amadeus_workdir/repos/03_Components/acf_ssl/br_12-0-1/src/SSLSocket.cpp", - "/home/kkwiatkowski/amadeus_workdir/repos/Tracer_br_12-0-0/test/data/Test100_shared", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/crypto/cryptlib.c", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/tools/c_rehash", - "/home/kkwiatkowski/storage/91_Repositories/srp_client_server_implementation_with_openssl/client.cpp", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/doc/ssl/SSL_set_verify_result.pod", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/doc/ssl/SSL_state_string.pod", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/VMS/openssl_startup.com", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/doc/crypto/OpenSSL_add_all_algorithms.pod", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/MacOS/OpenSSL.mcp.hqx", - "/home/kkwiatkowski/storage/91_Repositories/openssl101j/openssl.doxy" + "/home/kkwiatkowski/storage/91_Repositories/openssl101j/crypto/srp/srp_lib.c" ], "find": { @@ -1107,6 +1128,10 @@ "height": 90.0, "where_history": [ + "/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/", + "", + "/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/", + "/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto/utils", "/home/kkwiatkowski/amadeus_workdir/repos/02_SI/br_5-1/test/python/si", "/home/kkwiatkowski/amadeus_workdir/repos/03_Components/CMG/CMG_br_12-0", "/home/kkwiatkowski/storage/91_Repositories/openssl101j", @@ -1169,6 +1194,65 @@ "case_sensitive": false, "find_history": [ + "convert_hex", + "choose_min\nchoose_min\nchoose_min", + "choose_min", + "hex_str", + "xor_repe", + "xor_repeatedly", + "i_string_len", + "i_start_with", + "choose_min_block_size", + " ", + "04", + "1d421f4d0b0f021f4f134e3c1a69651f491c0e4e13010b074e1b01164536001e01496420541d1d4333534e6552060047541c0d454d07040c53123c0c1e08491a09114f144c211a472b00051d47591104090064260753003716060c1a17411d015254305f0020130a05474f124808454e653e160938450605081a46074f1f59787e6a62360c1d0f410d4806551a1b001d4274041e01491a091102527a7f4900483a001a13491a4f45480f1d0d53043a015219010b411306004c315f53621506070907540b17411416497933350b1b01050f46074f1d4e784e48275204070c4558480841004f205408740b1d194902000e165c00523069651f4902025400010b074e021053012610154d0207021f4f1b4e78306936520a010954060709534e021053083b100605490f0f104f3b003a5f472b1c4964334f540210531a4f051611740c064d0f020e0343524c3d4e002f0b490d084e170d15541d4f0e1c455e280b4d190112070a5553784e4f6206010b47531d0c0000170a051f0c3a425e4d2e0141220e1c493456416235064f472a7e3b084f011b01534237", + " ", + "decrypt_repeted_xor_test", + "[12]", + "[17]", + "[15]", + "12]", + "15", + "[6]", + "6", + "frequency", + "[6]", + "[5]", + "tmp_min_keysize", + "hex_buf", + "ciphertext_play", + "~", + "assert", + "ciphertext", + "counter", + "\n", + "HUIfTQsPAh9PE048GmllH0kcDk4TAQsHThsBFkU2AB4BSWQgVB0dQzNTTmVS", + "c2b", + "base64_3char_to_hex", + "hex_to_base64_test", + ",", + "0x61,", + " ", + "tmp[", + "tmp", + "fully_coppied", + "min_block_size", + "strlen(base64_buf)", + "hex_to_base64", + "po", + "hex_to_base64", + "convert_string_to", + "convert_hex_to", + " char* p_out_buf=out_buf;\n", + "convert_hex_to_string", + "convert_string", + "hex_to_base64", + " buf", + "buf", + "base64_to_hex", + "read_file_to_buffer", + "xor_repeate", + "xor_repeatedly", "xor_char_finder", "char_finder", "xor_char_finder", @@ -1237,66 +1321,7 @@ "DEBUG", "TRC1ASIEDI", "IN{cfg.srv[0].name}-TCIL", - "socket", - "ssl", - "SSLTrafficEventHandler", - "setHandshakeTimer", - "Leak", - "Memcheck:Leak", - "setSrpUsernameCallback", - "SRP_server_callback", - "PWD_CB_ARGS", - "USER_NAME", - "SSL_CTX_set_srp_password", - "srpCliPwdCb", - "USER_PASS", - "PWD_CB_ARGS", - "srpCliPwdCb", - "SRP_server_callback", - "setVerifyPeer", - "PWD_CB_ARGS", - "SRP_server_callback", - "SSL_CTX_set_srp_client_pwd_callback", - "setSrpPwdCallback", - "USER_NAME", - "PWD_CB_ARGS", - "testClientServerSRPAuthCallbacks", - "SSL_CTX_set_srp_username", - "SSL_set_srp_server_param", - "set_srp_username", - "SSL_CTX_set_srp_username", - "4", - "setCommonOptions", - "Server", - "SocketAddress", - "Socket", - "56667", - "SocketTest", - "handleClient", - "runClientHandler", - "handleClient", - "SAFE_BOOST_CHECK", - "SAFE_BOOST_REQUIRE", - "openssl_strdup", - "OPENSSL_str", - "SSL_CTX_set_srp_username_callback", - "argToAdd", - "_argTLS1Protocol", - "_argNoTLS1Protocol", - "mapArg(", - "mapArg", - "argToAdd", - "UncompressPayload", - "_argTLS11Protocol", - "_argUncompressPayload", - "default", - "11Proto", - "kAdminCmdType_New", - "mapArgFla", - "_argSSL3Protocol", - "mapArgFlag", - "_argSSL3Protocol", - "iInbound" + "socket" ], "highlight": true, "in_selection": false, @@ -1359,126 +1384,126 @@ "groups": [ { - "selected": 3, + "selected": 5, "sheets": [ { "buffer": 0, - "file": "utils/hamming.c", + "file": "set1/runner.cpp", "semi_transient": false, "settings": { - "buffer_size": 1465, + "buffer_size": 9656, "regions": { }, "selection": [ [ - 777, - 777 + 9184, + 9184 ] ], "settings": { - "function_name_status_row": 33, - "spell_check": false, + "function_name_status_row": 170, "syntax": "Packages/C++/C++.tmLanguage", "translate_tabs_to_spaces": false }, "translation.x": 0.0, - "translation.y": 0.0, + "translation.y": 1627.0, "zoom_level": 1.0 }, - "stack_index": 6, + "stack_index": 1, "type": "text" }, { "buffer": 1, - "file": "utils/hamming.h", + "file": "utils/base64.cpp", "semi_transient": false, "settings": { - "buffer_size": 190, + "buffer_size": 8006, "regions": { }, "selection": [ [ - 187, - 187 + 8006, + 8006 ] ], "settings": { - "function_name_status_row": 1, - "syntax": "Packages/C Improved/C Improved.tmLanguage" + "function_name_status_row": 258, + "syntax": "Packages/C++/C++.tmLanguage", + "translate_tabs_to_spaces": false }, "translation.x": 0.0, - "translation.y": 0.0, + "translation.y": 2689.0, "zoom_level": 1.0 }, - "stack_index": 5, + "stack_index": 2, "type": "text" }, { "buffer": 2, - "file": "set1/xor_char_finder.cpp", + "file": "utils/xor.c", "semi_transient": false, "settings": { - "buffer_size": 3565, + "buffer_size": 374, "regions": { }, "selection": [ [ - 538, - 538 + 374, + 374 ] ], "settings": { - "function_name_status_row": 25, - "syntax": "Packages/C++/C++.tmLanguage", - "translate_tabs_to_spaces": false + "function_name_status_row": 13, + "syntax": "Packages/C Improved/C Improved.tmLanguage" }, - "translation.x": 78.0, - "translation.y": 187.0, + "translation.x": 0.0, + "translation.y": 0.0, "zoom_level": 1.0 }, - "stack_index": 1, + "stack_index": 4, "type": "text" }, { "buffer": 3, - "file": "utils/xor.h", + "file": "set1/xor_char_finder.cpp", "semi_transient": true, "settings": { - "buffer_size": 98, + "buffer_size": 4443, "regions": { }, "selection": [ [ - 19, - 19 + 4443, + 4443 ] ], "settings": { - "function_name_status_row": 0, - "syntax": "Packages/C Improved/C Improved.tmLanguage" + "function_name_status_row": 146, + "syntax": "Packages/C++/C++.tmLanguage", + "translate_tabs_to_spaces": false }, - "translation.x": 0.0, - "translation.y": 0.0, + "translation.x": -0.0, + "translation.y": 1121.0, "zoom_level": 1.0 }, - "stack_index": 0, + "stack_index": 3, "type": "text" }, { @@ -1487,93 +1512,146 @@ "semi_transient": false, "settings": { - "buffer_size": 973, + "buffer_size": 3017, "regions": { }, "selection": [ [ - 868, - 868 + 3013, + 3013 ] ], "settings": { - "function_name_status_row": 42, - "syntax": "Packages/C Improved/C Improved.tmLanguage" + "function_name_status_row": 126, + "syntax": "Packages/C Improved/C Improved.tmLanguage", + "translate_tabs_to_spaces": false }, "translation.x": 0.0, - "translation.y": 0.0, + "translation.y": 855.0, "zoom_level": 1.0 }, - "stack_index": 3, + "stack_index": 6, "type": "text" }, { "buffer": 5, - "file": "utils/hex_to_base64.cpp", + "file": "utils/runner.cpp", "semi_transient": false, "settings": { - "buffer_size": 7716, + "buffer_size": 4173, "regions": { }, "selection": [ [ - 2403, - 2403 + 3260, + 3260 ] ], "settings": { - "function_name_status_row": 64, + "function_name_status_row": 106, "syntax": "Packages/C++/C++.tmLanguage", "translate_tabs_to_spaces": false }, "translation.x": 0.0, - "translation.y": 473.0, + "translation.y": 910.0, "zoom_level": 1.0 }, - "stack_index": 4, + "stack_index": 0, "type": "text" - } - ] - }, - { - "selected": 0, - "sheets": - [ + }, { "buffer": 6, - "file": "utils/utils_tester.cpp", + "file": "utils/common.h", "semi_transient": false, "settings": { - "buffer_size": 7345, + "buffer_size": 204, "regions": { }, "selection": [ [ - 6681, - 6681 + 56, + 56 ] ], "settings": { - "function_name_status_row": 239, - "syntax": "Packages/C++/C++.tmLanguage", - "translate_tabs_to_spaces": false + "function_name_status_row": 0, + "syntax": "Packages/C Improved/C Improved.tmLanguage" }, "translation.x": 0.0, - "translation.y": 3079.0, + "translation.y": 0.0, "zoom_level": 1.0 }, - "stack_index": 2, + "stack_index": 7, + "type": "text" + }, + { + "buffer": 7, + "file": "set1/runner.h", + "semi_transient": false, + "settings": + { + "buffer_size": 479, + "regions": + { + }, + "selection": + [ + [ + 274, + 274 + ] + ], + "settings": + { + "function_name_status_row": 14, + "syntax": "Packages/C Improved/C Improved.tmLanguage" + }, + "translation.x": 0.0, + "translation.y": 0.0, + "zoom_level": 1.0 + }, + "stack_index": 5, + "type": "text" + }, + { + "buffer": 8, + "semi_transient": false, + "settings": + { + "buffer_size": 443, + "regions": + { + }, + "selection": + [ + [ + 443, + 443 + ] + ], + "settings": + { + "auto_name": "This is an english text that is going to be encryp", + "default_dir": "/home/kkwiatkowski/storage/91_Repositories/MatasanoCrypto", + "function_name_status_row": 0, + "syntax": "Packages/Text/Plain text.tmLanguage" + }, + "translation.x": 0.0, + "translation.y": 0.0, + "zoom_level": 1.0 + }, + "stack_index": 8, "type": "text" } ] @@ -1596,12 +1674,6 @@ 0, 1, 1 - ], - [ - 0, - 1, - 1, - 2 ] ], "cols": @@ -1612,7 +1684,6 @@ "rows": [ 0.0, - 0.5, 1.0 ] }, @@ -1658,6 +1729,14 @@ "last_filter": "", "selected_items": [ + [ + "common.h", + "utils/common.h" + ], + [ + "xor.h", + "utils/xor.h" + ], [ "util", "utils/utils_tester.cpp" @@ -2161,14 +2240,6 @@ [ "cmdnewsetdelbasepeakversionte", "SI/masteragent/test/src/CmdNewSetDelBasePeakVersionTest.cpp" - ], - [ - "timer", - "HPT-13/src/TimersManager.cpp" - ], - [ - "cmdnewsetdelbasepeakversiontest", - "SI/masteragent/test/src/CmdNewSetDelBasePeakVersionTest.cpp" ] ], "width": 0.0 @@ -2475,7 +2546,7 @@ "show_open_files": true, "show_tabs": false, "side_bar_visible": true, - "side_bar_width": 212.0, + "side_bar_width": 189.0, "status_bar_visible": true, "template_settings": { diff --git a/etc/set1_t6.txt b/etc/set1_t6.txt index cecdb81..d073fb1 100644 --- a/etc/set1_t6.txt +++ b/etc/set1_t6.txt @@ -12,53 +12,4 @@ HQdJEwATARNFTg5JFwQ5C15NHQYEGk94dzBDADsdHE4UVBUaDE5JTwgHRTkA Umc6AUETCgYAN1xGYlUKDxJTEUgsAA0ABwcXOwlSGQELQQcbE0c9GioWGgwc AgcHSAtPTgsAABY9C1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gAAZDwGUwY7CBdN +HB5BFwMdUz0aQSwWSQoITlMcRUILTxoCEDUXF01jNw4BTwVBNlRBYhAIGhNM +EUgIRU5CRFMkOhwGBAQLTVQOHFkvUkUwF0lkbXkbHUVUBgAcFA0gRQYFCBpB +PU8FQSsaVycTAkJHYhsRSQAXABxUFzFFFggICkEDHR1OPxoqER1JDQhNEUgK +TkJPDAUAJhwQAg0XQRUBFgArU04lUh0GDlNUGwpOCU9jeTY1HFJARE4xGA4L +ACxSQTZSDxsJSw1ICFUdBgpTNjUcXk0OAUEDBxtUPRpCLQtFTgBPVB8NSRoK +SREKLUUVAklkERgOCwAsUkE2Ug8bCUsNSAhVHQYKUyI7RQUFABoEVA0dWXQa +Ry1SHgYOVBFIB08XQ0kUCnRvPgwQTgUbGBwAOVREYhAGAQBJEUgETgpPGR8E +LUUGBQgaQRIaHEshGk03AQANR1QdBAkAFwAcUwE9AFxNY2QxGA4LACxSQTZS +DxsJSw1ICFUdBgpTJjsIF00GAE1ULB1NPRpPLF5JAgJUVAUAAAYKCAFFXjUe +DBBOFRwOBgA+T04pC0kDElMdC0VXBgYdFkU2CgtNEAEUVBwTWXhTVG5SGg8e +AB0cRSo+AwgKRSANExlJCBQaBAsANU9TKxFJL0dMHRwRTAtPBRwQMAAATQcB +FlRlIkw5QwA2GggaR0YBBg5ZTgIcAAw3SVIaAQcVEU8QTyEaYy0fDE4ITlhI +Jk8DCkkcC3hFMQIEC0EbAVIqCFZBO1IdBgZUVA4QTgUWSR4QJwwRTWM= diff --git a/main.cpp b/main.cpp index 3f2ac87..5430abf 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,6 @@ int main() { - UTILS::run(); + // UTILS::run(); SET1::run(); } \ No newline at end of file diff --git a/set1/runner.cpp b/set1/runner.cpp index 83dba41..19aea8c 100644 --- a/set1/runner.cpp +++ b/set1/runner.cpp @@ -1,31 +1,61 @@ #include -#include #include #include -#include "set1/xor_char_finder.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() + +char guess_encryption_key_letter(const char ciphertext_hex[]) { - static const char ciphertext[] = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"; struct frequency_t max_score; + const int ciphertext_len = strlen(ciphertext_hex) / 2; + + unsigned char* ciphertext_xor = new unsigned char[ciphertext_len]; + convert_string_to_hex(ciphertext_hex, ciphertext_len*2, ciphertext_xor); + xor_char_finder(ciphertext_xor, max_score, ciphertext_len); + delete [] ciphertext_xor; + + return max_score.letter; +} - // ----------------------------------------------------------------------------- - // - // 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_challenge3_test() +{ + static const char ciphertext[] = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"; + guess_encryption_key_letter(ciphertext); + check(guess_encryption_key_letter(ciphertext) == 'X'); +} + + +void xor_char_finder_test() +{ + // "This is my simple text." + unsigned char buf[256]; + const char ciphertext_A[] = "15292832612832612c386132282c312d2461352439356f"; // A Encrypted + const char ciphertext_5[] = "615d5c46155c4615584c15465c584559501541504d411b"; // 5 Encrypted + const char ciphertext_h[] = "3c00011b48011b480511481b010518040d481c0d101c46"; // h Encrypted + + const char ciphertext_long[] = "1a252421286d223925283f6d28352e283d392422233e6d202c346d3e392421216d222e2e383f616d3925243e6d2028392522296d243e6d2e2c212128296d3e2c2b286d2f282e2c383e286d3e382f3e39243938392422233e6d2c213a2c343e6d393f24283e6d39226d3f2839383f236d2c6d383e2c2f21286d3e393f24232a6d24233e39282c296d222b6d3f2c243e24232a6d2c236d28352e283d39242223636d04236d2c23223925283f6d3e28233e28616d3e2c2b28123e382f3e39243938392865646d202c346d2f286d2c2334392524232a6d223925283f6d39252c236d3e2c2b28616d3e24232e286d24396d3a2421216d3e242128233921346d242a23223f286d202c212b223f2028296d3928203d212c39283e6d2e2223392c242324232a6d292c232a2124232a6d29282124202439283f3e616d3823202c392e2528296d2f3f2c2e283e616d223f6d3d212c2e2825222129283f3e6d39252c396d2c3f286d2322396d3b2c2124296d1d34392522236d2429282339242b24283f3e63"; // M + char got; + + got = guess_encryption_key_letter(ciphertext_A); + sprintf((char*)buf, "Returned: [%c]", got); + check(got == 'A', buf); + + got = guess_encryption_key_letter(ciphertext_5); + sprintf((char*)buf, "Returned: [%c]", got); + check(got == '5', buf); + + got = guess_encryption_key_letter(ciphertext_h); + sprintf((char*)buf, "Returned: [%c]", got); + check(got == 'h', buf); + + got = guess_encryption_key_letter(ciphertext_long); + sprintf((char*)buf, "Returned: [%c]", got); + check(got == 'M', buf); } void set1_challange4_test() @@ -38,12 +68,12 @@ void set1_challange4_test() unsigned char* p_hexdump = hexdump; fp=fopen("etc/set1_t4_input.txt", "r"); - if(fp==NULL) + if(fp==NULL) { fprintf(stderr, "etc/set1_t4_input.txt not found"); return; } - + while( getline(&line, &len, fp) != -1 ) { ++line_nb; @@ -55,12 +85,12 @@ void set1_challange4_test() // ----------------------------------------------------------------------------- // - // 1. Convert ASCII ciphertext to HEX + // 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; + 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); @@ -70,7 +100,7 @@ void set1_challange4_test() convert_string_to_hex(line, 60, p_hexdump); for(int i=0; i<30;++i) - { + { // printf("%c", hexdump[i]^max_score.letter); } // printf("\n"); @@ -83,53 +113,70 @@ void set1_challange4_test() 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[] = { + const unsigned char ch1[] = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"; + const unsigned char xor_val[] = "ICE"; + 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]; + unsigned char o_ch1[256]; memset(o_ch1, '\0', 256); unsigned char* po_ch1 = o_ch1; - - xor_repeatedly(xor_val, ch1, po_ch1); + + xor_repeatedly(xor_val, 3, ch1, sizeof(ch1), po_ch1); for(unsigned i=0; i %c %x\n", max_score.letter, max_score.letter); - } + 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); } + +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 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."; + + // password is 'short' + char hex_str1[] = "310108521612060852001b0d00000d4948201958530f00521c1c050a52170109150b541e0d015c54250d1d0b54040d031e5853214f11151d4f1b521f160d1f52010348181b001b481b1a1d00481c1717010d1b52151d11031d1a140d1d5c543c0343520d1c1d4f1e1d001c0a1c5407074f1f11530a1a06541a1c4801541d071b52151d114f1d125311000706530a1a01071a060a01075d48281d0053011b4d5424071d0100530a0a16540701021754001c00000d530d1917060a464f201114091d1618161b1c521b1548161d0101480917111f0101150753214f051b06040b52181a030a52001c480d001112034f051d07004f061c1a1b4f15010a46"; + + // Pass is 'DupaBladaPass' + char hex_str2[] = "101d191262051244003e41161d231919122a4c1501192441071b25015008314c060b083e0653072b55120462090f0713291107162055070836044109143c151a03281050022a0d130502240401006a5524092702412d413e04161764011f41351e08100470001f142b0719152a01411009311553042d191c4126090216182015531a305b50352a094113002941071c6411150230151110413915531a3755040e620a081612244110122816050d2318044409310c1e1a2a1250052b1f15050f3304531a2a551f1326091344153f411406210603412e090f031538411c15640118046207041d4f70351b1a37551d003b4c0705083c4d53113101500c231503014127081f1f64021f13294241370e70151b162a5539462e0041000433130a033055191562050f44123f0c165322141e023b4c16051870001d1764011800364b124408244f533a305507082e0041130e220a5f530d521d41311913014131031c06305519156c4c2e2f4d700f1c04643c570c62080e0a0470161a072c5504092b1f41171525111a17640115193642413704330e1d176410000831030501413f0753312d125023230206441538041c013d551912621f15051324081d146a"; + + // don't know what's the password but len is 20 + char hex_str3[] = "31060a450f0e1c024c1b06021f130a49572019485308024505001f004c0c1c060a18531e12015c44250a1f1c4d18170900434e2e5002121d501b520f160a1d45181f5212051b064704091a00571c1707010a19450c010b0903010902024f533c1c43521d1c1a4d09041c0600024f1a08500c1653151a06441a1b4a164d011d114c0e001e500e15530e000716530d18161e061c001f1c4047370e07531e1b4d4424001f16194f1000084f1a0e1d0453000300001d530a1b001f165c453e0a090602051f16041c520b154f140a181d5203090a020e1e0600533e4f050b06030945010619004c1b0147121316121c4f050d07074d11050601450b1a1749"; + + // this doesn't work. password is 'tie' + char hex_str4[] = "360002540b041a0e450001001b1b1c4e492a1f45451306451c060811490606081f0d490811074b543f00061045030c091845453d490615074200490e110c15541c15541e0c00014500010c074916110a17111d4515071c18060b130c175a492a1f45450d061054050c071d001a49111b4908114907011d451d1d4207490b1b1d4515071c54060354100a011b45161c1607000b111a165a49221b1d451d1d5a543e0a061a11540b001049111d0400541a111b1b1c540c13111b1c5a4937110e04060d09111a1654060354100a011b45120c0018000b131a453d49121b1c091049091d0200541d0a540b1711080e541e0c00014500010c07490201104b"; + + unsigned char buf[1024*1024]; + decrypt_repeted_xor(hex_str1, buf); + check(memcmp(expected_plaintext1, buf, sizeof(expected_plaintext1))==0, buf); + + decrypt_repeted_xor(hex_str2, 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); + + // decrypt_repeted_xor(hex_str4, buf); + // check(memcmp(expected_plaintext4, buf, sizeof(expected_plaintext4))==0, buf); +} \ No newline at end of file diff --git a/set1/runner.h b/set1/runner.h index 8e3d858..b57bc94 100644 --- a/set1/runner.h +++ b/set1/runner.h @@ -5,16 +5,21 @@ void set1_challenge3_test(); void set1_challange4_test(); void set1_challenge_5_test(); void set1_challenge_6_test(); +void xor_char_finder_test(); +void decrypt_repeted_xor_test(); -namespace SET1 { +namespace SET1 { void run() { - set1_challenge3_test(); - set1_challange4_test(); - set1_challenge_5_test(); + // decrypt_repeted_xor_test(); + + // xor_char_finder_test(); + // set1_challenge3_test(); + // set1_challange4_test(); + // set1_challenge_5_test(); set1_challenge_6_test(); - } + } } diff --git a/set1/xor_char_finder.cpp b/set1/xor_char_finder.cpp index 056a442..3cfaa07 100644 --- a/set1/xor_char_finder.cpp +++ b/set1/xor_char_finder.cpp @@ -1,14 +1,17 @@ #include #include +#include #include "utils/base64.h" +#include "utils/hamming.h" +#include "utils/xor.h" #include "set1/xor_char_finder.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 unsigned printable_ascii_len = sizeof(printable_ascii)/sizeof(char); - static const char printable_ascii[] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,-'+_`?!*:;$<>[]@#$%^&*()?>score); + score =tmp; } } } if(o_frequency.score > 0 && (score == o_frequency.score) ) { - printf("Two results with equal scores. Extend number of frequent letters.\n"); + printf("Two results with equal scores. Extend number of frequent letters\n"); return; } else if(score > o_frequency.score) @@ -96,7 +95,7 @@ void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequen o_frequency.score = score; o_frequency.letter = xorable_chars[i]; } - } + } } else if(xorable == 1) { @@ -104,5 +103,44 @@ void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequen o_frequency.frequency=0; o_frequency.score = 0; } +} + + +void decrypt_repeted_xor(const char* const i_hex_string, unsigned char* o_buf, const unsigned i_min_block ) +{ + unsigned len = strlen(i_hex_string)/2; + unsigned char* ciphertext_xor = new unsigned char[len]; + + 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); + fprintf(stdout, "Key Size: %d Hex Len: %d, Min key size: %d, SlideNb: %d\n", + keysize, + len, + keysize, + len/keysize); + + const unsigned int block_nb = len / keysize; + unsigned char* slide = new unsigned char[block_nb]; + + for(int j=0; j 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); -} \ No newline at end of file + delete [] slide; + delete [] ciphertext_xor; +} diff --git a/set1/xor_char_finder.h b/set1/xor_char_finder.h index a627b55..1d72184 100644 --- a/set1/xor_char_finder.h +++ b/set1/xor_char_finder.h @@ -1,9 +1,9 @@ -#define FREQUENT_LETTERS_AMOUNT 5 +#define FREQUENT_LETTERS_AMOUNT 26 struct frequency_t { - char letter; + unsigned char letter; unsigned long frequency; - unsigned long score; + unsigned long score; }; struct frequency_set { @@ -13,26 +13,110 @@ struct frequency_set { frequency_set() { - // Most frequent letters in english "etaoinshr", + // Most frequent letters in english "etaoinshr", frequencies[0].letter='e'; - frequencies[0].frequency=12; + frequencies[0].frequency=12702; frequencies[0].score=0; frequencies[1].letter='t'; - frequencies[1].frequency=9; + frequencies[1].frequency=9056; frequencies[1].score=0; frequencies[2].letter='a'; - frequencies[2].frequency=8; + frequencies[2].frequency=8167; frequencies[2].score=0; frequencies[3].letter='o'; - frequencies[3].frequency=8; + frequencies[3].frequency=7507; frequencies[3].score=0; frequencies[4].letter='i'; - frequencies[4].frequency=7; + frequencies[4].frequency=6966; frequencies[4].score=0; + + frequencies[5].letter='n'; + frequencies[5].frequency=6749; + frequencies[5].score=0; + + frequencies[6].letter='s'; + frequencies[6].frequency=6327; + frequencies[6].score=0; + + frequencies[7].letter='h'; + frequencies[7].frequency=6094; + frequencies[7].score=0; + + frequencies[8].letter='r'; + frequencies[8].frequency=5987; + frequencies[8].score=0; + + frequencies[9].letter='d'; + frequencies[9].frequency=4253; + frequencies[9].score=0; + + frequencies[10].letter='l'; + frequencies[10].frequency=4025; + frequencies[10].score=0; + + frequencies[11].letter='c'; + frequencies[11].frequency=2782; + frequencies[11].score=0; + + frequencies[12].letter='u'; + frequencies[12].frequency=2758; + frequencies[12].score=0; + + frequencies[13].letter='m'; + frequencies[13].frequency=2406; + frequencies[13].score=0; + + frequencies[14].letter='w'; + frequencies[14].frequency=2361; + frequencies[14].score=0; + + frequencies[15].letter='f'; + frequencies[15].frequency=2228; + frequencies[15].score=0; + + frequencies[16].letter='g'; + frequencies[16].frequency=2015; + frequencies[16].score=0; + + frequencies[17].letter='y'; + frequencies[17].frequency=1974; + frequencies[17].score=0; + + frequencies[18].letter='p'; + frequencies[18].frequency=1929; + frequencies[18].score=0; + + frequencies[19].letter='b'; + frequencies[19].frequency=1492; + frequencies[19].score=0; + + frequencies[20].letter='v'; + frequencies[20].frequency=978; + frequencies[20].score=0; + + frequencies[21].letter='k'; + frequencies[21].frequency=772; + frequencies[21].score=0; + + frequencies[22].letter='j'; + frequencies[22].frequency=153; + frequencies[22].score=0; + + frequencies[23].letter='x'; + frequencies[23].frequency=150; + frequencies[23].score=0; + + frequencies[24].letter='q'; + frequencies[24].frequency=95; + frequencies[24].score=0; + + frequencies[25].letter='z'; + frequencies[25].frequency=74; + frequencies[25].score=0; } const frequency_t& operator[](const unsigned idx) { return frequencies[idx]; @@ -40,3 +124,4 @@ struct frequency_set { }; 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 ); diff --git a/utils/base64.cpp b/utils/base64.cpp index bd8891e..849db2c 100644 --- a/utils/base64.cpp +++ b/utils/base64.cpp @@ -6,7 +6,7 @@ #define MIN(a,b) a96) // a... return T-71; //// 26=97-71 else if(T>64) // A... - return T-65; //// 0=65-65 - else if(T>47) // 0... - return T+5; //// 52=47+5 - else if(T==47) // / - return T+16; //// 63=47+16 - else if(T==43) // + - return T+21; //// 64=43+21 + return T-65; //// 0=65-65 + else if(T=='=') + return 64; + else if(T>=48) // 0... + return T+4; //// 52=47+5 + else if(T=='/') // / + return 63; + else if(T=='+') // + + return 62; + else + return 0xFF; } static const unsigned char int_to_base64[65] = { @@ -63,7 +75,7 @@ static const unsigned char int_to_base64[65] = { }; -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) { unsigned char hex; for(unsigned long i=0; i> 4 ); unsigned char b = ((c2b(T[1]) & 0xF ) << 4) | (c2b(T[2]) >> 2); @@ -211,17 +223,17 @@ struct int24 base64_3char_to_hex(const char (&T)[4]) } /* ----------------------------------------------------------------------------- - * @brief Converts string encoded in base64 to HEX - * - * @param i_string: Base64 encoded string. - * @param i_string_len: % 4 must == 0 - * @param o_hex_array: array to be populated with HEX's. Caller must ensure - * that array has required size. Method doesn't do any checks + * @brief Converts string encoded in base64 to HEX + * + * @param i_string: Base64 encoded string. + * @param i_string_len: % 4 must == 0 + * @param o_hex_array: array to be populated with HEX's. Caller must ensure + * that array has required size. Method doesn't do any checks * * @returns -1 on failure, 0 on success - * + * -------------------------------------------------------------------------------- */ -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 ) { int pointer = 0; int hex_pointer = 0; @@ -235,7 +247,7 @@ int base64_to_hex(const char* const i_string, int i_string_len, unsigned char*& array[pointer%4] = i_string[pointer]; pointer++; array[pointer%4] = i_string[pointer]; pointer++; - tmp = base64_3char_to_hex(array); + tmp = base64_4char_to_hex(array); o_hex_array[hex_pointer++] = tmp.data >> 16; o_hex_array[hex_pointer++] = tmp.data >> 8; o_hex_array[hex_pointer++] = tmp.data; diff --git a/utils/base64.h b/utils/base64.h index 2600321..d9c567a 100644 --- a/utils/base64.h +++ b/utils/base64.h @@ -1,6 +1,6 @@ -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 ); -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 xor_strings(const char* const iLeftString, const char* const iRightString, unsigned char*& oXorArray); +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 ); +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 xor_strings(const char* const iLeftString, const char* const iRightString, unsigned char* oXorArray); int c2b(char T); \ No newline at end of file diff --git a/utils/common.c b/utils/common.c index f51c23d..7eab1ba 100644 --- a/utils/common.c +++ b/utils/common.c @@ -2,7 +2,7 @@ #include #include -unsigned long read_file_to_buffer(const char* const filepath, char*& obuff, int remove_new_line_chr) +unsigned long read_file_to_buffer(const char* const filepath, char* obuff, int remove_new_line_chr) { FILE* fh=0; size_t len=0; @@ -28,3 +28,12 @@ unsigned long read_file_to_buffer(const char* const filepath, char*& obuff, int } return total_len; } + +void check(int iFlag, const unsigned char* const msg) +{ + if(!iFlag) + { + fprintf(stderr, "ERROR: %s\n", msg); + exit(1); + } +} diff --git a/utils/common.h b/utils/common.h index ff3e806..3f7e31f 100644 --- a/utils/common.h +++ b/utils/common.h @@ -1,3 +1,3 @@ +void check(int iFlag, const unsigned char* const msg = 0); struct int24 { unsigned int data:24; }; - -unsigned long read_file_to_buffer(const char* const filepath, char*& obuff, int remove_new_line_chr = 1); +unsigned long read_file_to_buffer(const char* const filepath, char* obuff, int remove_new_line_chr = 1); diff --git a/utils/hamming.c b/utils/hamming.c index 9ea7a75..a68318d 100644 --- a/utils/hamming.c +++ b/utils/hamming.c @@ -1,46 +1,44 @@ +#include #include #include #include "utils/base64.h" -static unsigned calculate_nb_of_bits_set(unsigned char i_char) -{ - static const unsigned len = 8; - unsigned char tmp_char = i_char; - unsigned idx = 0; - unsigned ret = 0; - for(;idx> 1; - } - return ret; -} - // calculate distance between s1 and s2 // -1 : failure (different sizes of the strings) -int block_distance(const char* const s1, const char* const s2, const unsigned i_size) +int block_distance(const unsigned char* const s1, const unsigned char* const s2, const unsigned i_size) { - int ret = 0; + int dist = 0; int counter = 0; for(counter=0; counter max dist is 40 - double tmp_dist1, tmp_dist2; - int min_block_size=40; + unsigned block_size = 0; + double min_dist=i_max_block_size*8; + int min_block_size = i_max_block_size; - tmp_dist1 = tmp_dist2 = 0; + // temporary results + double tmp_dist1, tmp_dist2, tmp_dist3; + tmp_dist1 = tmp_dist2 = tmp_dist3 = 0; - for(; block_size<=40; block_size++) + for(block_size=i_min_block_size; block_size<=i_max_block_size; block_size++) { // take 4 blocks, calc distance, normalize and get min @@ -48,13 +46,31 @@ int choose_min_block_size(const char* const i_string, const unsigned i_size, uns tmp_dist1 = tmp_dist1/block_size; 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 -#include -void xor_repeatedly(const char* const i_xor, const char* const i_string, unsigned char* o_xored) +void xor_repeatedly(const unsigned char* const i_xor, const unsigned i_xor_len, const unsigned char* const i_string, const unsigned i_string_len, unsigned char* o_xored) { - const unsigned xor_len = strlen(i_xor); unsigned counter = 0; - while( counter < strlen(i_string) ) + while( counter < i_string_len ) { - unsigned xor_idx = counter % xor_len; + unsigned xor_idx = counter % i_xor_len; o_xored[counter] = (i_string[counter]) ^ (i_xor[xor_idx]); counter++; - } + } } + diff --git a/utils/xor.h b/utils/xor.h index 0650aa3..3b4e636 100644 --- a/utils/xor.h +++ b/utils/xor.h @@ -1 +1 @@ -void xor_repeatedly(const char* const i_xor, const char* const i_string, unsigned char* o_xored); +void xor_repeatedly(const unsigned char* const i_xor, const unsigned i_xor_len, const unsigned char* const i_string, const unsigned i_string_len, unsigned char* o_xored); \ No newline at end of file