選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

set1.cpp 12 KiB

9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
9年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include "src/xor_char_finder.h"
  2. #include "src/common.h"
  3. #include "src/base64.h"
  4. #include "src/xor.h"
  5. #include "src/hamming.h"
  6. #include "src/enc_modes.h"
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #define SET1_T4_INPUT_FILE "sol/etc/set1_t4_input.txt"
  11. #define SET1_T7_INPUT_FILE "sol/etc/set1_t7.txt"
  12. #define SET1_T8_INPUT_FILE "sol/etc/set1_t8.txt"
  13. char guess_encryption_key_letter(const char ciphertext_hex[])
  14. {
  15. struct frequency_t max_score;
  16. const int ciphertext_len = strlen(ciphertext_hex) / 2;
  17. unsigned char* ciphertext_xor = new unsigned char[ciphertext_len];
  18. convert_string_to_hex(ciphertext_hex, ciphertext_len*2, ciphertext_xor);
  19. xor_char_finder(ciphertext_xor, max_score, ciphertext_len);
  20. delete [] ciphertext_xor;
  21. return max_score.letter;
  22. }
  23. TCASE(set1_challenge3_test)
  24. {
  25. static const char ciphertext[] = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736";
  26. unsigned char buf[2];
  27. char ret;
  28. ret = guess_encryption_key_letter(ciphertext);
  29. sprintf((char*)buf, "%c", ret);
  30. CHECK(ret == 'X', buf);
  31. }
  32. TCASE_E
  33. TCASE(xor_char_finder_test)
  34. {
  35. // "This is my simple text."
  36. unsigned char buf[256];
  37. const char ciphertext_A[] = "15292832612832612c386132282c312d2461352439356f"; // A Encrypted
  38. const char ciphertext_5[] = "615d5c46155c4615584c15465c584559501541504d411b"; // 5 Encrypted
  39. const char ciphertext_h[] = "3c00011b48011b480511481b010518040d481c0d101c46"; // h Encrypted
  40. const char ciphertext_long[] = "1a252421286d223925283f6d28352e283d392422233e6d202c346d3e392421216d222e2e383f616d3925243e6d2028392522296d243e6d2e2c212128296d3e2c2b286d2f282e2c383e286d3e382f3e39243938392422233e6d2c213a2c343e6d393f24283e6d39226d3f2839383f236d2c6d383e2c2f21286d3e393f24232a6d24233e39282c296d222b6d3f2c243e24232a6d2c236d28352e283d39242223636d04236d2c23223925283f6d3e28233e28616d3e2c2b28123e382f3e39243938392865646d202c346d2f286d2c2334392524232a6d223925283f6d39252c236d3e2c2b28616d3e24232e286d24396d3a2421216d3e242128233921346d242a23223f286d202c212b223f2028296d3928203d212c39283e6d2e2223392c242324232a6d292c232a2124232a6d29282124202439283f3e616d3823202c392e2528296d2f3f2c2e283e616d223f6d3d212c2e2825222129283f3e6d39252c396d2c3f286d2322396d3b2c2124296d1d34392522236d2429282339242b24283f3e63"; // M
  41. char got;
  42. got = guess_encryption_key_letter(ciphertext_A);
  43. sprintf((char*)buf, "Returned: [%c]", got);
  44. CHECK(got == 'A', buf);
  45. got = guess_encryption_key_letter(ciphertext_5);
  46. sprintf((char*)buf, "Returned: [%c]", got);
  47. CHECK(got == '5', buf);
  48. got = guess_encryption_key_letter(ciphertext_h);
  49. sprintf((char*)buf, "Returned: [%c]", got);
  50. CHECK(got == 'h', buf);
  51. got = guess_encryption_key_letter(ciphertext_long);
  52. sprintf((char*)buf, "Returned: [%c]", got);
  53. CHECK(got == 'M', buf);
  54. }
  55. TCASE_E
  56. TCASE(set1_challange4_test)
  57. {
  58. FILE* fp = NULL;
  59. char* line=NULL;
  60. size_t len=0;
  61. int line_nb = 0;
  62. unsigned char hexdump[30];
  63. unsigned char* p_hexdump = hexdump;
  64. fp=fopen(SET1_T4_INPUT_FILE, "r");
  65. if(fp==NULL)
  66. {
  67. fprintf(stderr, SET1_T4_INPUT_FILE " not found");
  68. goto end;
  69. }
  70. while( getline(&line, &len, fp) != -1 )
  71. {
  72. ++line_nb;
  73. // printf("%s",line);
  74. struct frequency_t max_score;
  75. max_score.letter=0x0;
  76. max_score.score = 0;
  77. // -----------------------------------------------------------------------------
  78. //
  79. // 1. Convert ASCII ciphertext to HEX
  80. //
  81. // -----------------------------------------------------------------------------
  82. const int ciphertext_len = strlen(line) / 2;
  83. unsigned char ciphertext_xor[ciphertext_len];
  84. unsigned char* p_ciphertext_xor = ciphertext_xor;
  85. convert_string_to_hex(line, ciphertext_len*2, p_ciphertext_xor);
  86. xor_char_finder(p_ciphertext_xor, max_score, ciphertext_len);
  87. if(max_score.letter != 0x00)
  88. {
  89. // printf("%d -> %c\n", line_nb, max_score.letter);
  90. convert_string_to_hex(line, 60, p_hexdump);
  91. for(int i=0; i<30;++i)
  92. {
  93. // printf("%c", hexdump[i]^max_score.letter);
  94. }
  95. // printf("\n");
  96. }
  97. free(line); line=0; len=0;
  98. }
  99. end:
  100. fclose(fp);
  101. free(line);
  102. }
  103. TCASE_E
  104. TCASE(set1_challenge_5_test)
  105. {
  106. const unsigned char ch1[] = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal";
  107. const unsigned char xor_val[] = "ICE";
  108. const unsigned char expected[] = {
  109. 0x0b,0x36,0x37,0x27,0x2a,0x2b,0x2e,0x63,0x62,0x2c,0x2e,0x69,0x69,0x2a,0x23,0x69,
  110. 0x3a,0x2a,0x3c,0x63,0x24,0x20,0x2d,0x62,0x3d,0x63,0x34,0x3c,0x2a,0x26,0x22,0x63,
  111. 0x24,0x27,0x27,0x65,0x27,0x2a,0x28,0x2b,0x2f,0x20,0x43,0x0a,0x65,0x2e,0x2c,0x65,
  112. 0x2a,0x31,0x24,0x33,0x3a,0x65,0x3e,0x2b,0x20,0x27,0x63,0x0c,0x69,0x2b,0x20,0x28,
  113. 0x31,0x65,0x28,0x63,0x26,0x30,0x2e,0x27,0x28,0x2f};
  114. unsigned char o_ch1[256];
  115. memset(o_ch1, '\0', 256);
  116. unsigned char* po_ch1 = o_ch1;
  117. xor_repeatedly(xor_val, 3, ch1, sizeof(ch1), po_ch1, 256);
  118. for(unsigned i=0; i<sizeof(expected)/sizeof(unsigned char); ++i)
  119. CHECK( expected[i] == po_ch1[i] );
  120. }
  121. TCASE_E
  122. TCASE(set1_challenge_6_test)
  123. {
  124. const unsigned max_block_size=200;
  125. char base64_buf[1024*1024]; // 1 MB
  126. uint8_t* pbuff = (uint8_t*) &base64_buf[0];
  127. char error_buf[256];
  128. unsigned char out_buf[1024*1024];
  129. unsigned char hex_buf[1024*1024];
  130. unsigned long len=read_file_to_buffer("sol/etc/set1_t6.txt", &pbuff, &len);
  131. base64_buf[len]='\0';
  132. len=base64_to_hex((unsigned char*)base64_buf, len, hex_buf);
  133. convert_hex_to_string(hex_buf, len, base64_buf);
  134. // now base64_buf contains string with ciphertext that is HEX-encoded
  135. memset(out_buf,0,sizeof(out_buf));
  136. int best_key_size = find_best_keysize(base64_buf, strlen(base64_buf));
  137. CHECK(best_key_size==29);
  138. best_key_size=crack_repeted_xor(base64_buf, out_buf, best_key_size);
  139. // CHECK(memcmp(expected_plaintext1, out_buf, sizeof(expected_plaintext1))==0, out_buf);
  140. // OZAPTF: to do, it works just need correct check
  141. // CHECK(memcmp(expected_plaintext3, buf, sizeof(expected_plaintext3))==0, buf);
  142. }
  143. TCASE_E
  144. TCASE(crack_repeted_xor_test)
  145. {
  146. unsigned char error_buf[256];
  147. 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.";
  148. 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.";
  149. 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.";
  150. 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.";
  151. // password is 'short'
  152. char hex_str1[] = "310108521612060852001b0d00000d4948201958530f00521c1c050a52170109150b541e0d015c54250d1d0b54040d031e5853214f11151d4f1b521f160d1f52010348181b001b481b1a1d00481c1717010d1b52151d11031d1a140d1d5c543c0343520d1c1d4f1e1d001c0a1c5407074f1f11530a1a06541a1c4801541d071b52151d114f1d125311000706530a1a01071a060a01075d48281d0053011b4d5424071d0100530a0a16540701021754001c00000d530d1917060a464f201114091d1618161b1c521b1548161d0101480917111f0101150753214f051b06040b52181a030a52001c480d001112034f051d07004f061c1a1b4f15010a46";
  153. // Pass is 'DupaBladaPass'
  154. char hex_str2[] = "101d191262051244003e41161d231919122a4c1501192441071b25015008314c060b083e0653072b55120462090f0713291107162055070836044109143c151a03281050022a0d130502240401006a5524092702412d413e04161764011f41351e08100470001f142b0719152a01411009311553042d191c4126090216182015531a305b50352a094113002941071c6411150230151110413915531a3755040e620a081612244110122816050d2318044409310c1e1a2a1250052b1f15050f3304531a2a551f1326091344153f411406210603412e090f031538411c15640118046207041d4f70351b1a37551d003b4c0705083c4d53113101500c231503014127081f1f64021f13294241370e70151b162a5539462e0041000433130a033055191562050f44123f0c165322141e023b4c16051870001d1764011800364b124408244f533a305507082e0041130e220a5f530d521d41311913014131031c06305519156c4c2e2f4d700f1c04643c570c62080e0a0470161a072c5504092b1f41171525111a17640115193642413704330e1d176410000831030501413f0753312d125023230206441538041c013d551912621f15051324081d146a";
  155. // don't know what's the password but len is 20
  156. char hex_str3[] = "31060a450f0e1c024c1b06021f130a49572019485308024505001f004c0c1c060a18531e12015c44250a1f1c4d18170900434e2e5002121d501b520f160a1d45181f5212051b064704091a00571c1707010a19450c010b0903010902024f533c1c43521d1c1a4d09041c0600024f1a08500c1653151a06441a1b4a164d011d114c0e001e500e15530e000716530d18161e061c001f1c4047370e07531e1b4d4424001f16194f1000084f1a0e1d0453000300001d530a1b001f165c453e0a090602051f16041c520b154f140a181d5203090a020e1e0600533e4f050b06030945010619004c1b0147121316121c4f050d07074d11050601450b1a1749";
  157. // this doesn't work. password is 'tie'
  158. char hex_str4[] = "360002540b041a0e450001001b1b1c4e492a1f45451306451c060811490606081f0d490811074b543f00061045030c091845453d490615074200490e110c15541c15541e0c00014500010c074916110a17111d4515071c18060b130c175a492a1f45450d061054050c071d001a49111b4908114907011d451d1d4207490b1b1d4515071c54060354100a011b45161c1607000b111a165a49221b1d451d1d5a543e0a061a11540b001049111d0400541a111b1b1c540c13111b1c5a4937110e04060d09111a1654060354100a011b45120c0018000b131a453d49121b1c091049091d0200541d0a540b1711080e541e0c00014500010c07490201104b";
  159. unsigned char buf[1024*1024];
  160. memset(buf,0,sizeof(buf));
  161. int best_key_size = find_best_keysize(hex_str1, strlen(hex_str1));
  162. crack_repeted_xor(hex_str1, buf, best_key_size);
  163. sprintf((char*)error_buf, "Wrong keysize. Expected 5 got %d\n", best_key_size);
  164. CHECK(best_key_size==5, error_buf);
  165. CHECK(memcmp(expected_plaintext1, buf, sizeof(expected_plaintext1))==0, buf);
  166. // here I have to reduce key size otherwise with keylength 39 there is better score
  167. memset(buf,0,sizeof(buf));
  168. best_key_size = find_best_keysize(hex_str2, strlen(hex_str2),2,38);
  169. crack_repeted_xor(hex_str2, buf, best_key_size);
  170. sprintf((char*)error_buf, "Wrong keysize. Expected 13 got %d\n", best_key_size);
  171. CHECK(best_key_size==13, error_buf);
  172. CHECK(memcmp(expected_plaintext2, buf, sizeof(expected_plaintext2))==0, buf);
  173. // here I have to reduce key size otherwise with keylength 39 there is better score
  174. memset(buf,0,sizeof(buf));
  175. best_key_size = find_best_keysize(hex_str3, strlen(hex_str3));
  176. crack_repeted_xor(hex_str3, buf, best_key_size);
  177. sprintf((char*)error_buf, "Wrong keysize. Expected 20 got %d\n", best_key_size);
  178. CHECK(best_key_size==20, error_buf);
  179. CHECK(memcmp(expected_plaintext3, buf, sizeof(expected_plaintext3))==0, buf);
  180. }
  181. TCASE_E
  182. TCASE(set1_challenge_7_test)
  183. {
  184. const char expected_first_line[] = "I'm back and I'm ringin' the bell";
  185. Result_t res = Result_Error;
  186. CryptoAttribs_t attribs;
  187. Key_t key;
  188. CryptoAttribs_t::Init(&attribs);
  189. Key_t::Init(&key);
  190. key.len = 16;
  191. key.key = (uint8_t*) malloc(key.len * sizeof(uint8_t));
  192. memcpy(key.key, "YELLOW SUBMARINE", key.len);
  193. res = load_base64_to_hex(
  194. SET1_T7_INPUT_FILE,
  195. &attribs.input,
  196. &(attribs.input_len));
  197. CHECK(res == Result_OK, (uint8_t*) "Error while loading ciphertext");
  198. ecb_decrypt(&attribs, &key);
  199. CHECK(
  200. memcmp( attribs.output,
  201. expected_first_line,
  202. strlen(expected_first_line))==0, attribs.output);
  203. Key_t::Free(&key);
  204. CryptoAttribs_t::Free(&attribs);
  205. }
  206. TCASE_E
  207. TCASE(set1_challenge_8_test)
  208. {
  209. FILE* fh=NULL;
  210. if( (fh=fopen(SET1_T8_INPUT_FILE, "rb")) == 0 )
  211. {
  212. fprintf(stderr, "File %s doesn't exist. Exiting...\n", SET1_T8_INPUT_FILE);
  213. exit(1);
  214. }
  215. int first,second;
  216. char* line=NULL;
  217. size_t len=0;
  218. int bytes_read=0;
  219. int current_line=0;
  220. while( (bytes_read=getline(&line, &len, fh)) != -1 )
  221. {
  222. ++current_line;
  223. if(same_blocks(line, 32, &first, &second))
  224. {
  225. break;
  226. }
  227. free(line);
  228. line=NULL;
  229. }
  230. free(line);
  231. fclose(fh);
  232. CHECK(current_line==133);
  233. CHECK(first==2);
  234. CHECK(second==4);
  235. }
  236. TCASE_E