Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. goodCharacters = [ chr(z) for (f,t) in [('a', 'z'), ('0', '9'), (' ', ' ')] for z in range(ord(f),ord(t)+1) ]
  4. #goodCharacters = [ chr(z) for (f,t) in [('a', 'z')] for z in range(ord(f),ord(t)+1) ]
  5.  
  6. def encrypt(plaintext, key):
  7. ciphertext = ""
  8. last = 0
  9. for j in range(len(plaintext)):
  10. c = plaintext[j]
  11. i = goodCharacters.index(c)
  12. i = (i + key[j%len(key)] + j + last) % len(goodCharacters)
  13. #i = (i + key[j%len(key)]) % len(goodCharacters)
  14. cp = goodCharacters[i]
  15. ciphertext = ciphertext + cp
  16. last = i
  17. return ciphertext
  18.  
  19. def to_original(encrypted):
  20. ciphertext = ""
  21. last = 0
  22. aux_last = 0
  23. for j in range(len(encrypted)):
  24. c = encrypted[j]
  25. i = goodCharacters.index(c)
  26. aux_last = i
  27. i = (i - j - last) % len(goodCharacters)
  28. last = aux_last
  29.  
  30. cp = goodCharacters[i]
  31. ciphertext = ciphertext + cp
  32. return ciphertext
  33.  
  34. def decrypt(plaintext, key):
  35. ciphertext = ""
  36. last = 0
  37. for j in range(len(plaintext)):
  38. c = plaintext[j]
  39. i = goodCharacters.index(c)
  40. i = (i - key[j%len(key)]) % len(goodCharacters)
  41. cp = goodCharacters[i]
  42. ciphertext = ciphertext + cp
  43. last = i
  44. return ciphertext
  45.  
  46.  
  47. def get_max(dict):
  48. maximum = 0;
  49. key = 0;
  50. for k,v in dict.items():
  51. #print (k)
  52. if maximum < v:
  53. maximum = v
  54. key = k
  55.  
  56. print(key)
  57.  
  58. def print_distances(encrypted):
  59. print("%20s%20s%20s%20s" % ("Repeated Bigram","Location","Distance","Factors") )
  60.  
  61. values_list = dict()
  62.  
  63. for i in range(1, len(plaintext)):
  64. pair = encrypted[i-1] + encrypted[i]
  65. for j in range(i, len(plaintext)-1):
  66. nextpair = encrypted[j] + encrypted[j+1]
  67. if pair == nextpair:
  68. dist = j-i+1
  69. #print("%20s%20d%20d" % (pair, j, j-i+1) )
  70. if dist in values_list:
  71. values_list[dist] = values_list[dist] + 1
  72. if dist == 3:
  73. print ("dist: " + str(dist) + " value: " + str(values_list[dist]))
  74. else:
  75. values_list[dist] = 1;
  76.  
  77. break
  78.  
  79. get_max(values_list)
  80.  
  81. def bruteforce(original):
  82. key = ""
  83.  
  84. the_occurrences = 0
  85. count = 0
  86.  
  87. for i in range(len(goodCharacters)):
  88. for j in range(len(goodCharacters)):
  89. for k in range(len(goodCharacters)):
  90. count = count + 1
  91. if count % 100 == 0:
  92. print(str(count) + " out of " + str(len(goodCharacters)**3))
  93. key = [i, j, k]
  94. plaintext = decrypt(original, key)
  95. the_occurrences = plaintext.count(" the ")
  96. if the_occurrences > 10:
  97. print ("FOR KEY: " + str(key))
  98. print (plaintext)
  99.  
  100. plaintext = ""
  101. f = open("cipher.txt", "r")
  102. for line in f:
  103. line = line.lower()
  104. line = line.strip()
  105. line = line + " "
  106. plaintext = plaintext + "".join([c for c in line if c in goodCharacters])
  107.  
  108. f.close()
  109.  
  110. # you need to figure out the real key
  111. #print encrypt(plaintext, [1,2,3,4,5,6,7,8,9,10])
  112. #key = [17,4,11,0,19,8,14,13,18] #RELATIONS
  113. #encrypted = encrypt(plaintext, key)
  114. # print (encrypted)
  115.  
  116. original = to_original(plaintext)
  117. # print(original)
  118.  
  119. bruteforce(original)
  120.  
  121. #print_distances(original)
  122.  
  123. # decrypted = decrypt(original, key)
  124. # print (decrypted)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement