Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):
  2. bits = bin(int.from_bytes(text.encode(encoding, errors), 'big'))[2:]
  3. return bits.zfill(8 * ((len(bits) + 7) // 8))
  4.  
  5.  
  6. def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):
  7. n = int(bits, 2)
  8. try :
  9. return n.to_bytes((n.bit_length() + 7) // 8, 'big').decode(encoding, errors) or ''
  10. except:
  11. return "Not processable"
  12.  
  13.  
  14.  
  15. def xor(bit1, bit2):
  16. result = ''
  17. for idx, val in enumerate(bit1):
  18. result = result + ('0' if (bit1[idx] == bit2[idx]) else '1')
  19. return result
  20.  
  21.  
  22. def crib_brute(bit_str, crib_word_bit):
  23. for i in range(50):
  24. xor_result = xor(crib_word_bit, bit_str[i: len(crib_word_bit) + i])
  25. print(str(i) + ": " + text_from_bits(xor_result))
  26.  
  27.  
  28. c1 = "1010110010011110011111101110011001101100111010001111011101101011101000110010011000000101001110111010010111100100111101001010000011000001010001001001010000000010101001000011100100010011011011011011010111010011000101010111111110010011010111001001010101110001111101010000001011110100000000010010111001111010110000001101010010110101100010011111111011101101001011111001101111101111000100100001000111101111011011001011110011000100011111100001000101111000011101110101110010010100010111101111110011011011001101110111011101100110010100010001100011001010100110001000111100011011001000010101100001110011000000001110001011101111010100101110101000100100010111011000001111001110000011111111111110010111111000011011001010010011100011100001011001101110110001011101011101111110100001111011011000110001011111111101110110101101101001011110110010111101000111011001111"
  29. c2 = "1011110110100110000001101000010111001000110010000110110001101001111101010000101000110100111010000010011001100100111001101010001001010001000011011001010100001100111011010011111100100101000001001001011001110010010100101011111010001110010010101111110001100010100001110000110001111111001000100001001010100011100100001101010101111000100001111101111110111001000101111111101011001010000100100000001011001001010000101001110101110100001111100001011101100100011000110111110001000100010111110110111010010010011101011111111001011011001010010110100100011001010110110001001000100011011001110111010010010010110100110100000111100001111101111010011000100100110011111011001010101000100000011111010010110111001100011100001111100100110010010001111010111011110110001000111101010110101001110111001110111010011111111010100111000100111001011000111101111101100111011001111"
  30.  
  31. crib_word = "the"
  32. crib_word_bit = text_to_bits(crib_word)
  33.  
  34. crib_brute(xor(c1, c2), crib_word_bit)
  35.  
  36. 0: eP
  37. 1: Not processable
  38. 2: Not processable
  39. 3: Not processable
  40. 4: Not processable
  41. 5: Sgi
  42. 6: :v}
  43. 7: Not processable
  44. 8: L
  45. 9: Not processable
  46. 10: Not processable
  47. 11: Not processable
  48. 12: Not processable
  49. 13: {d
  50. 14: Not processable
  51. 15: Not processable
  52.  
  53. 1 53.10% #####################################################
  54. 2 53.15% #####################################################
  55. 3 53.14% #####################################################
  56. 4 53.32% #####################################################
  57. 5 53.10% #####################################################
  58. 6 53.09% #####################################################
  59. 7 58.77% ###########################################################
  60. 8 53.28% #####################################################
  61. 9 52.98% #####################################################
  62. 10 53.08% #####################################################
  63. 11 53.01% #####################################################
  64. 12 53.27% #####################################################
  65. 13 52.73% #####################################################
  66. 14 58.60% ###########################################################
  67. 15 53.04% #####################################################
  68. 16 53.22% #####################################################
  69. 17 52.86% #####################################################
  70. 18 53.02% #####################################################
  71. 19 52.91% #####################################################
  72. 20 53.46% #####################################################
  73. 21 58.74% ###########################################################
  74. 22 52.95% #####################################################
  75. 23 52.58% #####################################################
  76. 24 53.60% ######################################################
  77. 25 53.34% #####################################################
  78. 26 52.65% #####################################################
  79. 27 52.78% #####################################################
  80. 28 59.19% ###########################################################
  81. 29 52.58% #####################################################
  82. 30 52.72% #####################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement