Bläddra i källkod

T6 not working yet

api_change
Krzysztof KWIATKOWSKI 9 år sedan
förälder
incheckning
f835c605b8
19 ändrade filer med 878 tillägg och 499 borttagningar
  1. +43
    -1
      MatsanoCrypto
  2. +285
    -214
      MatsanoCrypto.sublime-workspace
  3. +1
    -50
      etc/set1_t6.txt
  4. +64
    -0
      etc/set1_t6.txt.orig
  5. +1
    -1
      main.cpp
  6. +102
    -55
      set1/runner.cpp
  7. +10
    -5
      set1/runner.h
  8. +63
    -25
      set1/xor_char_finder.cpp
  9. +94
    -9
      set1/xor_char_finder.h
  10. +58
    -46
      utils/base64.cpp
  11. +5
    -5
      utils/base64.h
  12. +10
    -1
      utils/common.c
  13. +2
    -2
      utils/common.h
  14. +43
    -27
      utils/hamming.c
  15. +5
    -2
      utils/hamming.h
  16. +80
    -45
      utils/runner.cpp
  17. +6
    -4
      utils/runner.h
  18. +5
    -6
      utils/xor.c
  19. +1
    -1
      utils/xor.h

+ 43
- 1
MatsanoCrypto Visa fil

@@ -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
}
}

+ 285
- 214
MatsanoCrypto.sublime-workspace Visa fil

@@ -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/",
"<current file>",
"/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":
{


+ 1
- 50
etc/set1_t6.txt Visa fil

@@ -12,53 +12,4 @@ HQdJEwATARNFTg5JFwQ5C15NHQYEGk94dzBDADsdHE4UVBUaDE5JTwgHRTkA
Umc6AUETCgYAN1xGYlUKDxJTEUgsAA0ABwcXOwlSGQELQQcbE0c9GioWGgwc
AgcHSAtPTgsAABY9C1VNCAINGxgXRHgwaWUfSQcJABkRRU8ZAUkDDTUWF01j
OgkRTxVJKlZJJwFJHQYADUgRSAsWSR8KIgBSAAxOABoLU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HYhsRSQAXABxUFzFFFggICkEDHR1OPxoqER1JDQhNEUgK
TkJPDAUAJhwQAg0XQRUBFgArU04lUh0GDlNUGwpOCU9jeTY1HFJARE4xGA4L
ACxSQTZSDxsJSw1ICFUdBgpTNjUcXk0OAUEDBxtUPRpCLQtFTgBPVB8NSRoK
SREKLUUVAklkERgOCwAsUkE2Ug8bCUsNSAhVHQYKUyI7RQUFABoEVA0dWXQa
Ry1SHgYOVBFIB08XQ0kUCnRvPgwQTgUbGBwAOVREYhAGAQBJEUgETgpPGR8E
LUUGBQgaQRIaHEshGk03AQANR1QdBAkAFwAcUwE9AFxNY2QxGA4LACxSQTZS
DxsJSw1ICFUdBgpTJjsIF00GAE1ULB1NPRpPLF5JAgJUVAUAAAYKCAFFXjUe
DBBOFRwOBgA+T04pC0kDElMdC0VXBgYdFkU2CgtNEAEUVBwTWXhTVG5SGg8e
AB0cRSo+AwgKRSANExlJCBQaBAsANU9TKxFJL0dMHRwRTAtPBRwQMAAATQcB
FlRlIkw5QwA2GggaR0YBBg5ZTgIcAAw3SVIaAQcVEU8QTyEaYy0fDE4ITlhI
Jk8DCkkcC3hFMQIEC0EbAVIqCFZBO1IdBgZUVA4QTgUWSR4QJwwRTWM=

+ 1
- 1
main.cpp Visa fil

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

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

+ 102
- 55
set1/runner.cpp Visa fil

@@ -1,31 +1,61 @@
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>

#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<sizeof(expected)/sizeof(unsigned char); ++i)
assert( expected[i] == po_ch1[i] );
check( expected[i] == po_ch1[i] );
}

void set1_challenge_6_test()
{
char buf[1024*1024]; // 1 MB
const unsigned max_block_size=200;

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

for(int i=0; i<len; ++i)
buf[i]=hex_buf[i];
buf[len]='\0';

// guess keysize
int keysize = choose_min_block_size(buf, strlen(buf), 2);
unsigned char* slide = new unsigned char[strlen(buf)/keysize];
for(int j=0; j<keysize; ++j)
{
memset(slide, '\0', strlen(buf)/keysize);
int counter = 0;
for(int i=0; i+j<strlen(buf); i+=keysize, counter++)
{
slide[counter] = hex_buf[i+j];
}
struct frequency_t max_score;
xor_char_finder(slide, max_score, counter);
printf("> %c %x\n", max_score.letter, max_score.letter);
}
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);
}

+ 10
- 5
set1/runner.h Visa fil

@@ -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();
}
}

}



+ 63
- 25
set1/xor_char_finder.cpp Visa fil

@@ -1,14 +1,17 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
#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.,-'+_`?!*:;$<>[]@#$%^&*()?><MZ}|\"{:P";
static const unsigned printable_ascii_len = sizeof(printable_ascii)/sizeof(char);
// -----------------------------------------------------------------------------
//
// Array keeps letters that occure most frequently in english language. They are
@@ -16,18 +19,12 @@ void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequen
//
// -----------------------------------------------------------------------------
frequency_set frequency;
/*
printf("Frequencies ");
for(int i=0; i<FREQUENT_LETTERS_AMOUNT; ++i)
printf("%c %d, ",frequency[i].letter, frequency[i].frequency);
printf("\n");
*/


// -----------------------------------------------------------------------------
//
//
// Keeps xorable characters - those which produce valid ascii text after xoring
// with ciphertext.
// with ciphertext.
//
// -----------------------------------------------------------------------------
int xorable = 0;
@@ -37,27 +34,26 @@ void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequen
// -----------------------------------------------------------------------------
//
// 2. Check if Xorable. Get those characters for which XOR of ciphertext and
// each byte from cipher text produces some valid text.
// each byte from cipher text produces some valid text.
//
// -----------------------------------------------------------------------------
unsigned char tmp[printable_ascii_len][printable_ascii_len];
// -----------------------------------------------------------------------------
for(int i=0; i<printable_ascii_len; ++i)
{
char tmp_hex[ciphertext_len];
char aXorChar = printable_ascii[i];
int fully_coppied=1;
int whole_ciphertext_xorable=1; // whole ciphertext is xorable with aXorChar
for(int j=0; j<ciphertext_len; ++j)
{
const char xor_chr = p_ciphertext_xor[j]^aXorChar;
if( strchr(printable_ascii, xor_chr) == NULL)
{
fully_coppied=0;
whole_ciphertext_xorable=0;
break;
}
tmp[xorable][j] = xor_chr;
}
if(fully_coppied)
if(whole_ciphertext_xorable)
{
// printf("Xorable %c\n", aXorChar);
xorable_chars[xorable] = aXorChar;
xorable++;
}
@@ -65,7 +61,7 @@ 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.
// of letters from frequency list.
//
// -----------------------------------------------------------------------------
o_frequency.frequency=0;
@@ -75,20 +71,23 @@ void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequen
{
for(int i=0; i<xorable; ++i)
{
unsigned long score=0;
unsigned long long score=0;
for(int j=0; j<ciphertext_len; ++j)
{
for(int k=0; k<FREQUENT_LETTERS_AMOUNT; ++k)
{
if(frequency[k].letter == (p_ciphertext_xor[j]^xorable_chars[i]))
{
score += frequency[k].frequency;
unsigned long long tmp = score + frequency[k].frequency;
// crash if overflow
assert(tmp>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<keysize; ++j)
{
memset(slide, '\0', block_nb);
int i=0;
for(; i<block_nb; ++i)
{
slide[i] = ciphertext_xor[i*keysize + j];
}

struct frequency_t max_score;
xor_char_finder(slide, max_score, block_nb);

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

+ 94
- 9
set1/xor_char_finder.h Visa fil

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

+ 58
- 46
utils/base64.cpp Visa fil

@@ -6,7 +6,7 @@

#define MIN(a,b) a<b ? a : b

static const unsigned short char_to_hex[256] = {
static const unsigned short char_to_hex[256] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -16,11 +16,16 @@ static const unsigned short char_to_hex[256] = {
0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0b, 0x0c,
0x0d, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

// 100: this rest shouldn't be needed
// 100: this rest shouldn't be needed
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -30,26 +35,33 @@ static const unsigned short char_to_hex[256] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};


/* -----------------------------------------------------------------------------
* @brief Converts ASCII char to index in Base64 char
*
* @param T ascii char
*
* @returns Can return 0xFF if char not found, otherwise index of a char
-------------------------------------------------------------------------------- */
int c2b(char T)
{
if(T>96) // 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<iArrayLen; i+=2)
@@ -72,13 +84,13 @@ void convert_string_to_hex(const char* const iArray, const unsigned iArrayLen, u
hex += char_to_hex[ iArray[i+1] ];

oHexArray[i/2]=hex;
}
}
}

void convert_hex_to_string(const unsigned char* const iHexArray, unsigned iHexArrayLen, char*& oStringBuf )
void convert_hex_to_string(const unsigned char* const iHexArray, unsigned iHexArrayLen, char* oStringBuf )
{
for(unsigned i=0; i<iHexArrayLen; i++)
{
{
sprintf(oStringBuf+(i*2), "%x", iHexArray[i]);
}
}
@@ -96,26 +108,26 @@ void read_bytes(const char* hex_buff, struct int24& o_data, unsigned len=3)
}

/* -----------------------------------------------------------------------------
* @brief Converts HEX to Base64
*
* @brief Converts HEX to Base64
*
* @param hex_buf : buffer with hexes to be converted
* base64_buff : output buffer. has to be \0 initialized
* base64_buff : output buffer. has to be \0 initializedhttps://www.facebook.com/iz.ka.735
* hex_buff_len: length of hex_buff
*
*
* @returns number of characters written to base64_buf
*
* @remarks remarks
* @remarks remarks
*
*------------------------------------------------------------------------------ */
unsigned hex_to_base64(const char* hex_buff, char*& base64_buff, unsigned hex_buff_len )
{
unsigned hex_to_base64(const char* hex_buff, char* base64_buff, unsigned hex_buff_len )
{
unsigned j=0;
const unsigned unaligned = hex_buff_len % 6;
const unsigned unaligned = hex_buff_len % 6;
unsigned len_aligned = hex_buff_len - unaligned;

int24 data;
unsigned char idx1,idx2,idx3,idx4;
for(unsigned i=0; i<len_aligned; i+=6)
for(unsigned i=0; i<len_aligned; i+=6)
{
data.data = 0;
read_bytes(hex_buff+i, data, 3);
@@ -141,7 +153,7 @@ unsigned hex_to_base64(const char* hex_buff, char*& base64_buff, unsigned hex_bu
{
if( ( data.data & (data.data & 0x3F) ) == data.data)
{
base64_buff[j++] = int_to_base64[ data.data ];
base64_buff[j++] = int_to_base64[ data.data ];
}
else
{
@@ -166,23 +178,23 @@ unsigned hex_to_base64(const char* hex_buff, char*& base64_buff, unsigned hex_bu
}

/* -----------------------------------------------------------------------------
* @brief xor_strings
*
* @brief xor_strings
*
* @param iLeftString : String of HEX numbers. Length of string needs to be even
* @param iRightString: Second string. Has to be same length as iLeftString
* @param oXorArray : Array will keep result of XOR. Array needs to be pre
* allocated with length at least strlen(iLeftString)/2
*
* @remarks remarks
*
* @remarks remarks
*
-------------------------------------------------------------------------------- */
void xor_strings(const char* const iLeftString, const char* const iRightString, unsigned char*& oXorArray)
void xor_strings(const char* const iLeftString, const char* const iRightString, unsigned char* oXorArray)
{
const size_t aInLen = strlen(iLeftString);
// Both arrays need to have same length
if(strlen(iRightString) != aInLen) return;
// Length of both tables needs to be even
if(aInLen % 2 != 0) return;

@@ -200,7 +212,7 @@ void xor_strings(const char* const iLeftString, const char* const iRightString,
free(xor_array2);
}

struct int24 base64_3char_to_hex(const char (&T)[4])
struct int24 base64_4char_to_hex(const char (&T)[4])
{
unsigned char a = ( c2b(T[0]) << 2 ) | (c2b(T[1]) >> 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;


+ 5
- 5
utils/base64.h Visa fil

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

+ 10
- 1
utils/common.c Visa fil

@@ -2,7 +2,7 @@
#include <stdio.h>
#include <string.h>

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

+ 2
- 2
utils/common.h Visa fil

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

+ 43
- 27
utils/hamming.c Visa fil

@@ -1,46 +1,44 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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<len; idx++)
{
ret += (tmp_char & 0x01);
tmp_char = tmp_char >> 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<i_size; ++counter)
{
ret += calculate_nb_of_bits_set(s1[counter]^s2[counter]);
// calc nb bits set
unsigned char ch = s1[counter]^s2[counter];
while(ch!=0)
{
dist++;
ch &= ch - 1; // this is neet. Wegner technique for counting ones in binary computer
// http://gurmeet.net/puzzles/fast-bit-counting-routines/
}
}

return ret;
return dist;
}

int choose_min_block_size(const char* const i_string, const unsigned i_size, unsigned i_start_with)
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 = i_start_with;
double min_dist=40; // max block is 40 => 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<min_dist) {
min_dist = tmp_dist1;
min_block_size = block_size;
}*/

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

}
return min_block_size;
}

+ 5
- 2
utils/hamming.h Visa fil

@@ -1,2 +1,5 @@
int block_distance(const char* const s1, const char* const s2, const unsigned i_size);
int choose_min_block_size(const char* const i_string, const unsigned i_size, unsigned i_start_with);
int block_distance(const unsigned char* const s1, const unsigned char* const s2, const unsigned i_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);

+ 80
- 45
utils/runner.cpp Visa fil

@@ -6,101 +6,136 @@
#include "utils/base64.h"
#include "utils/hamming.h"
#include "utils/xor.h"
#include "utils/common.h"
#include "set1/xor_char_finder.h"

void hex_to_base64_test() {
const char test_buff1[] ="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";
const char test_buff1[] ="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";
const char expected_buff1[] ="SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t";

const char test_buff2[] ="4927";
const char test_buff2[] ="4927";
const char expected_buff2[] ="SSH";

const char test_buff3[] ="49";
const char test_buff3[] ="49";
const char expected_buff3[] ="SB";

const char test_buff4[] ="33";
const char test_buff4[] ="33";
const char expected_buff4[] ="z";

const char test_buff5[] = "616e79";
const char expected_buff5[] = "YW55";

char output[256];
char* po=output;

memset (output,'\0', 256);
hex_to_base64(test_buff1, po, strlen(test_buff1) );
assert( memcmp(expected_buff1, output, strlen(expected_buff1)) == 0 );
assert( strlen(expected_buff1) == strlen(output));
hex_to_base64(test_buff1, output, strlen(test_buff1) );
check( memcmp(expected_buff1, output, strlen(expected_buff1)) == 0 );
check( strlen(expected_buff1) == strlen(output));
// test case when there are 2 hex'es
memset (output,'\0', 256);
hex_to_base64(test_buff2, po, strlen(test_buff2) );
assert( memcmp(expected_buff2, output, strlen(expected_buff2)) == 0 );
assert( strlen(expected_buff2) == strlen(output));
hex_to_base64(test_buff2, output, strlen(test_buff2) );
check( memcmp(expected_buff2, output, strlen(expected_buff2)) == 0 );
check( strlen(expected_buff2) == strlen(output));

// test case when there is 1 hex
memset (output,'\0', 256);
hex_to_base64(test_buff3, po, strlen(test_buff3) );
assert( memcmp(expected_buff3, output, strlen(expected_buff3)) == 0 );
assert( strlen(expected_buff3) == strlen(output));
hex_to_base64(test_buff3, output, strlen(test_buff3) );
check( memcmp(expected_buff3, output, strlen(expected_buff3)) == 0 );
check( strlen(expected_buff3) == strlen(output));

// test case when there is 1 char
memset (output,'\0', 256);
hex_to_base64(test_buff4, po, strlen(test_buff4) );
assert( memcmp(expected_buff4, output, strlen(expected_buff4)) == 0 );
assert( strlen(expected_buff4) == strlen(output));
hex_to_base64(test_buff4, output, strlen(test_buff4) );
check( memcmp(expected_buff4, output, strlen(expected_buff4)) == 0 );
check( strlen(expected_buff4) == strlen(output));

memset (output,'\0', 256);
hex_to_base64(test_buff5, output, strlen(test_buff5) );
check( memcmp(expected_buff5, output, strlen(expected_buff5)) == 0 );
check( strlen(expected_buff5) == strlen(output));
}

void convert_string_to_hex_test()
{
const char test_buff1[] = "49276d";
unsigned char out_buff[3];
unsigned char* p = out_buff;
convert_string_to_hex(test_buff1, strlen(test_buff1), p);
assert( p[0] == 0x49
&& p[1] == 0x27
&& p[2] == 0x6d);
unsigned char out_buff[3];
convert_string_to_hex(test_buff1, strlen(test_buff1), out_buff);
check( out_buff[0] == 0x49
&& out_buff[1] == 0x27
&& out_buff[2] == 0x6d);

// and back
char out_buf[6];
char* op = out_buf;
convert_hex_to_string(p, 3, op);
assert( memcmp(op, test_buff1, 6) == 0 );
convert_hex_to_string(out_buff, 3, out_buf);
check( memcmp(out_buf, test_buff1, 6) == 0 );

}

void xor_strings_test()
{
{
const char i_buf_1[] = "1c0111001f010100061a024b53535009181c";
const char* p_ibuf1=i_buf_1;

const char i_buf_2[] = "686974207468652062756c6c277320657965";
const char* p_ibuf2=i_buf_2;

char out_buf[36];
char* p_out_buf=out_buf;

unsigned char buf[36/2];
unsigned char* p_buf = buf;

xor_strings(p_ibuf1, p_ibuf2, p_buf);
xor_strings(i_buf_1, i_buf_2, buf);

convert_hex_to_string(buf, 36/2, p_out_buf);
assert( memcmp(out_buf, "746865206b696420646f6e277420706c6179", 36) == 0);
convert_hex_to_string(buf, 36/2, out_buf);
check( memcmp(out_buf, "746865206b696420646f6e277420706c6179", 36) == 0);
}

void hamming_test()
{
static const char ch1[]="this is a test";
static const char ch2[]="wokka wokka!!!";
assert(block_distance(ch1,ch2,strlen(ch1)) == 37);
unsigned char msg[256];
const unsigned char ch1[]="this is a test";
const unsigned char ch2[]="wokka wokka!!!";
sprintf((char*)msg, "GOT: %d", block_distance(ch1, ch2,14));
check(block_distance(ch1,ch2, 14) == 37, msg);

const unsigned char ch3[]="test1";
const unsigned char ch4[]="test3";

sprintf((char*)msg, "GOT: %d", block_distance(ch3, ch4,5));
check(block_distance(ch3,ch4,5)==1, msg);


const unsigned char ch5[]={0x01, 0x03};
const unsigned char ch6[]={0x02, 0x02};

sprintf((char*)msg, "GOT: %d", block_distance(ch5, ch6,2));
check(block_distance(ch5,ch6,2)==3, msg);

}

void base64_to_hex_test()
{
static const char ch2[]="TWFuTWFu"; // 0x41 0x61 0x6E 0x41 0x61 0x6E
static const char ch3[]="YW55IGNhcm5hbCBwbGVhc3U=";
unsigned char out[6];
unsigned char *p=out;
unsigned char out3[17];

unsigned char expected[6] = {0x4D, 0x61, 0x6E, 0x4D, 0x61, 0x6E};
base64_to_hex(ch2, 8, p);
assert( memcmp(expected, out, 6) == 0 );
unsigned char expected3[] = {0x61, 0x6e, 0x79, 0x20, 0x63, 0x61, 0x72, 0x6e, 0x61, 0x6c, 0x20, 0x70, 0x6c, 0x65, 0x61, 0x73, 0x75};
base64_to_hex(ch2, 8, out);
base64_to_hex(ch3, 17, out3);

check( memcmp(expected, out, 6) == 0 );
check( memcmp(expected3, out3, 6) == 0 );
}

void c2b_test()
{
check( c2b('A') == 0 );
check( c2b('L') == 11 );
check( c2b('Z') == 25 );
check( c2b('a') == 26 );
check( c2b('z') == 51 );
check( c2b('0') == 52 );
check( c2b('5') == 57 );
check( c2b('9') == 61 );
check( c2b('+') == 62 );
check( c2b('/') == 63 );
check( c2b('=') == 64 );
check( c2b('*') == 0xFF );
}

+ 6
- 4
utils/runner.h Visa fil

@@ -2,19 +2,21 @@
#define __utils_run__

void hex_to_base64_test();
unsigned long read_file_to_buffer(const char* const filepath, char*& obuff, int remove_new_line_chr = 1);
void xor_strings_test();
void hamming_test();
void base64_to_hex_test();
void convert_string_to_hex_test();
void c2b_test();

namespace UTILS {
void run() {
void run() {
hamming_test();
hex_to_base64_test();
// convert_string_to_hex_test();
convert_string_to_hex_test();
xor_strings_test();
hamming_test();
base64_to_hex_test();
c2b_test();
}

}


+ 5
- 6
utils/xor.c Visa fil

@@ -1,14 +1,13 @@
#include <string.h>
#include <stdio.h>

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++;
}
}
}


+ 1
- 1
utils/xor.h Visa fil

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

Laddar…
Avbryt
Spara