Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

182 рядки
9.4 KiB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "set1/xor_char_finder.h"
  5. #include "utils/common.h"
  6. #include "utils/base64.h"
  7. #include "utils/xor.h"
  8. #include "utils/hamming.h"
  9. char guess_encryption_key_letter(const char ciphertext_hex[])
  10. {
  11. struct frequency_t max_score;
  12. const int ciphertext_len = strlen(ciphertext_hex) / 2;
  13. unsigned char* ciphertext_xor = new unsigned char[ciphertext_len];
  14. convert_string_to_hex(ciphertext_hex, ciphertext_len*2, ciphertext_xor);
  15. xor_char_finder(ciphertext_xor, max_score, ciphertext_len);
  16. delete [] ciphertext_xor;
  17. return max_score.letter;
  18. }
  19. void set1_challenge3_test()
  20. {
  21. static const char ciphertext[] = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736";
  22. guess_encryption_key_letter(ciphertext);
  23. check(guess_encryption_key_letter(ciphertext) == 'X');
  24. }
  25. void xor_char_finder_test()
  26. {
  27. // "This is my simple text."
  28. unsigned char buf[256];
  29. const char ciphertext_A[] = "15292832612832612c386132282c312d2461352439356f"; // A Encrypted
  30. const char ciphertext_5[] = "615d5c46155c4615584c15465c584559501541504d411b"; // 5 Encrypted
  31. const char ciphertext_h[] = "3c00011b48011b480511481b010518040d481c0d101c46"; // h Encrypted
  32. const char ciphertext_long[] = "1a252421286d223925283f6d28352e283d392422233e6d202c346d3e392421216d222e2e383f616d3925243e6d2028392522296d243e6d2e2c212128296d3e2c2b286d2f282e2c383e286d3e382f3e39243938392422233e6d2c213a2c343e6d393f24283e6d39226d3f2839383f236d2c6d383e2c2f21286d3e393f24232a6d24233e39282c296d222b6d3f2c243e24232a6d2c236d28352e283d39242223636d04236d2c23223925283f6d3e28233e28616d3e2c2b28123e382f3e39243938392865646d202c346d2f286d2c2334392524232a6d223925283f6d39252c236d3e2c2b28616d3e24232e286d24396d3a2421216d3e242128233921346d242a23223f286d202c212b223f2028296d3928203d212c39283e6d2e2223392c242324232a6d292c232a2124232a6d29282124202439283f3e616d3823202c392e2528296d2f3f2c2e283e616d223f6d3d212c2e2825222129283f3e6d39252c396d2c3f286d2322396d3b2c2124296d1d34392522236d2429282339242b24283f3e63"; // M
  33. char got;
  34. got = guess_encryption_key_letter(ciphertext_A);
  35. sprintf((char*)buf, "Returned: [%c]", got);
  36. check(got == 'A', buf);
  37. got = guess_encryption_key_letter(ciphertext_5);
  38. sprintf((char*)buf, "Returned: [%c]", got);
  39. check(got == '5', buf);
  40. got = guess_encryption_key_letter(ciphertext_h);
  41. sprintf((char*)buf, "Returned: [%c]", got);
  42. check(got == 'h', buf);
  43. got = guess_encryption_key_letter(ciphertext_long);
  44. sprintf((char*)buf, "Returned: [%c]", got);
  45. check(got == 'M', buf);
  46. }
  47. void set1_challange4_test()
  48. {
  49. FILE* fp;
  50. char* line=NULL;
  51. size_t len=0;
  52. int line_nb = 0;
  53. unsigned char hexdump[30];
  54. unsigned char* p_hexdump = hexdump;
  55. fp=fopen("etc/set1_t4_input.txt", "r");
  56. if(fp==NULL)
  57. {
  58. fprintf(stderr, "etc/set1_t4_input.txt not found");
  59. return;
  60. }
  61. while( getline(&line, &len, fp) != -1 )
  62. {
  63. ++line_nb;
  64. // printf("%s",line);
  65. struct frequency_t max_score;
  66. max_score.letter=0x0;
  67. max_score.score = 0;
  68. // -----------------------------------------------------------------------------
  69. //
  70. // 1. Convert ASCII ciphertext to HEX
  71. //
  72. // -----------------------------------------------------------------------------
  73. const int ciphertext_len = strlen(line) / 2;
  74. unsigned char ciphertext_xor[ciphertext_len];
  75. unsigned char* p_ciphertext_xor = ciphertext_xor;
  76. convert_string_to_hex(line, ciphertext_len*2, p_ciphertext_xor);
  77. xor_char_finder(p_ciphertext_xor, max_score, ciphertext_len);
  78. if(max_score.letter != 0x00)
  79. {
  80. // printf("%d -> %c\n", line_nb, max_score.letter);
  81. convert_string_to_hex(line, 60, p_hexdump);
  82. for(int i=0; i<30;++i)
  83. {
  84. // printf("%c", hexdump[i]^max_score.letter);
  85. }
  86. // printf("\n");
  87. }
  88. free(line); line=0; len=0;
  89. }
  90. free(line);
  91. }
  92. void set1_challenge_5_test()
  93. {
  94. const unsigned char ch1[] = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal";
  95. const unsigned char xor_val[] = "ICE";
  96. const unsigned char expected[] = {
  97. 0x0b,0x36,0x37,0x27,0x2a,0x2b,0x2e,0x63,0x62,0x2c,0x2e,0x69,0x69,0x2a,0x23,0x69,
  98. 0x3a,0x2a,0x3c,0x63,0x24,0x20,0x2d,0x62,0x3d,0x63,0x34,0x3c,0x2a,0x26,0x22,0x63,
  99. 0x24,0x27,0x27,0x65,0x27,0x2a,0x28,0x2b,0x2f,0x20,0x43,0x0a,0x65,0x2e,0x2c,0x65,
  100. 0x2a,0x31,0x24,0x33,0x3a,0x65,0x3e,0x2b,0x20,0x27,0x63,0x0c,0x69,0x2b,0x20,0x28,
  101. 0x31,0x65,0x28,0x63,0x26,0x30,0x2e,0x27,0x28,0x2f};
  102. unsigned char o_ch1[256];
  103. memset(o_ch1, '\0', 256);
  104. unsigned char* po_ch1 = o_ch1;
  105. xor_repeatedly(xor_val, 3, ch1, sizeof(ch1), po_ch1);
  106. for(unsigned i=0; i<sizeof(expected)/sizeof(unsigned char); ++i)
  107. check( expected[i] == po_ch1[i] );
  108. }
  109. void set1_challenge_6_test()
  110. {
  111. const unsigned max_block_size=200;
  112. char base64_buf[1024*1024]; // 1 MB
  113. unsigned char out_buf[1024*1024];
  114. unsigned char hex_buf[1024*1024];
  115. unsigned long len=read_file_to_buffer("etc/set1_t6.txt", base64_buf);
  116. base64_buf[len]='\0';
  117. len=base64_to_hex(base64_buf, len, hex_buf);
  118. convert_hex_to_string(hex_buf, len, base64_buf);
  119. printf("%s\n", base64_buf);
  120. decrypt_repeted_xor(base64_buf, out_buf);
  121. }
  122. void decrypt_repeted_xor_test()
  123. {
  124. 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.";
  125. 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.";
  126. 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.";
  127. 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.";
  128. // password is 'short'
  129. char hex_str1[] = "310108521612060852001b0d00000d4948201958530f00521c1c050a52170109150b541e0d015c54250d1d0b54040d031e5853214f11151d4f1b521f160d1f52010348181b001b481b1a1d00481c1717010d1b52151d11031d1a140d1d5c543c0343520d1c1d4f1e1d001c0a1c5407074f1f11530a1a06541a1c4801541d071b52151d114f1d125311000706530a1a01071a060a01075d48281d0053011b4d5424071d0100530a0a16540701021754001c00000d530d1917060a464f201114091d1618161b1c521b1548161d0101480917111f0101150753214f051b06040b52181a030a52001c480d001112034f051d07004f061c1a1b4f15010a46";
  130. // Pass is 'DupaBladaPass'
  131. char hex_str2[] = "101d191262051244003e41161d231919122a4c1501192441071b25015008314c060b083e0653072b55120462090f0713291107162055070836044109143c151a03281050022a0d130502240401006a5524092702412d413e04161764011f41351e08100470001f142b0719152a01411009311553042d191c4126090216182015531a305b50352a094113002941071c6411150230151110413915531a3755040e620a081612244110122816050d2318044409310c1e1a2a1250052b1f15050f3304531a2a551f1326091344153f411406210603412e090f031538411c15640118046207041d4f70351b1a37551d003b4c0705083c4d53113101500c231503014127081f1f64021f13294241370e70151b162a5539462e0041000433130a033055191562050f44123f0c165322141e023b4c16051870001d1764011800364b124408244f533a305507082e0041130e220a5f530d521d41311913014131031c06305519156c4c2e2f4d700f1c04643c570c62080e0a0470161a072c5504092b1f41171525111a17640115193642413704330e1d176410000831030501413f0753312d125023230206441538041c013d551912621f15051324081d146a";
  132. // don't know what's the password but len is 20
  133. char hex_str3[] = "31060a450f0e1c024c1b06021f130a49572019485308024505001f004c0c1c060a18531e12015c44250a1f1c4d18170900434e2e5002121d501b520f160a1d45181f5212051b064704091a00571c1707010a19450c010b0903010902024f533c1c43521d1c1a4d09041c0600024f1a08500c1653151a06441a1b4a164d011d114c0e001e500e15530e000716530d18161e061c001f1c4047370e07531e1b4d4424001f16194f1000084f1a0e1d0453000300001d530a1b001f165c453e0a090602051f16041c520b154f140a181d5203090a020e1e0600533e4f050b06030945010619004c1b0147121316121c4f050d07074d11050601450b1a1749";
  134. // this doesn't work. password is 'tie'
  135. char hex_str4[] = "360002540b041a0e450001001b1b1c4e492a1f45451306451c060811490606081f0d490811074b543f00061045030c091845453d490615074200490e110c15541c15541e0c00014500010c074916110a17111d4515071c18060b130c175a492a1f45450d061054050c071d001a49111b4908114907011d451d1d4207490b1b1d4515071c54060354100a011b45161c1607000b111a165a49221b1d451d1d5a543e0a061a11540b001049111d0400541a111b1b1c540c13111b1c5a4937110e04060d09111a1654060354100a011b45120c0018000b131a453d49121b1c091049091d0200541d0a540b1711080e541e0c00014500010c07490201104b";
  136. unsigned char buf[1024*1024];
  137. decrypt_repeted_xor(hex_str1, buf);
  138. check(memcmp(expected_plaintext1, buf, sizeof(expected_plaintext1))==0, buf);
  139. decrypt_repeted_xor(hex_str2, buf);
  140. check(memcmp(expected_plaintext2, buf, sizeof(expected_plaintext2))==0, buf);
  141. // decrypt_repeted_xor(hex_str3, buf, 4);
  142. // memset(buf, 0, 1024*1024);
  143. //check(memcmp(expected_plaintext3, buf, sizeof(expected_plaintext3))==0, buf);
  144. // decrypt_repeted_xor(hex_str4, buf);
  145. // check(memcmp(expected_plaintext4, buf, sizeof(expected_plaintext4))==0, buf);
  146. }