Guest User

Untitled

a guest
Oct 16th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import re
  4. import os
  5. import sys
  6. import random
  7. from itertools import chain, product
  8.  
  9. testkey = 'q w e r'
  10. message = 'ta kzxwgpdjtctpf prozif, d nop zs adpgidc agxjoin tn kdccom iokgintho, kzxwgpdjco zi moktmdjco ts proio tn da dcyzitprx ertkr poixtadpon dspoi d statpo dxzgap zs ptxo (ertkr xdf mowoam za pro ythoa agxjoi) dam kziiokpcf moktmon eroproi d ythoa agxjoi joczayn pz pro nop.'
  11. sampledecrypt = 'ly dsourjwclqlje jxisbe, w hij sk ywjrbwq yrocibh lh dwqqig bidrbhlai, dsourjwcqi sb gidlgwcqi lk jxibi lh wy wqpsbljxo vxldx jibolywjih wkjib w klylji wosryj sk jloi (vxldx owe giuiyg sy jxi plaiy yrocib) wyg dsbbidjqe gidlgih vxijxib w plaiy yrocib ciqsyph js jxi hij.'
  12. messagetarget = 'In computability theory, a set of natural numbers is called recursive, computable or decidable if there is an algorithm which terminates after a finite amount of time (which may depend on the given number) and correctly decides whether a given number belongs to the set.'.lower()
  13. alpha = 'abcdefghijklmnopqrstuvwxyz'
  14.  
  15. '''
  16. we first need to generate the master key (e50) from the initial key
  17. seed the state (d) by concatenating the alphabet onto the key (all lowercase).
  18. state = asdfabcdefghijklmnopqrstuvwxyz
  19. loop through each letter of the alphabet.
  20. call that letter's index (1-indexed) a
  21. and call the letter itself b.
  22. find that letter's position (1-indexed) in the state, this is c.
  23.  
  24. to update the state, we need to generate d_left and d_right.
  25. d_left is the first c chars of the current state
  26. d_right is the remainder of the chars (position c to the end)
  27. with all occurrences of b deleted.
  28.  
  29. we update the state with the concatenation of d_left and d_right
  30.  
  31. to encrypt, we operate char by char.
  32. we first get the index of the first occurrence of the plaintext char
  33. in the master key, add 20 to that index, and call it findval.
  34. the encrypted char is the findval'th character of e50
  35. '''
  36.  
  37. def gen_e50(key):
  38. col_a = [i for i in range(1, 271)] #
  39. col_b = list(alpha) # =char(A51+96)
  40. col_c = [] # =FIND(B51,D50)
  41. col_d = [] # =CONCAT(LEFT(D50,C51), SUBSTITUTE(MID(D50,C51+1, LEN(D50)), B51, ""))
  42.  
  43. d50 = re.sub('[^a-z]', '', key.lower()[0:10]) + alpha
  44. for a in col_a[0:len(col_b)]:
  45. if a == 1:
  46. d_prev = d50
  47. else:
  48. d_prev = col_d[a-2]
  49. b = col_b[a-1]
  50. c = d_prev.find(b) + 1
  51. d_right = d_prev[c:len(d_prev)]
  52. d_right = re.sub(b, '', d_right)
  53. d_left = d_prev[0:c]
  54. d = d_left + d_right
  55. col_c.append(c)
  56. col_d.append(d)
  57.  
  58. e50 = col_d[-1] + col_d[-1]
  59. return e50
  60.  
  61. def encryptchar(char, e50):
  62. # =IFERROR(MID($E$50, FIND(E51, $E$50)+20, 1), E51)
  63. if not char in e50:
  64. return char
  65. findval = e50.find(char) + 20
  66. return e50[findval]
  67.  
  68. def scoredecrypt(key):
  69. testpt = ''.join([encryptchar(char, gen_e50(key)) for char in message])
  70. score = 0
  71. for c in range(len(testpt)):
  72. if testpt[c] == messagetarget[c]:
  73. score += 1
  74. return score
  75.  
  76. assert gen_e50(testkey) == 'qwerabcdfghijklmnopstuvxyzqwerabcdfghijklmnopstuvxyz'
  77. e50 = gen_e50(testkey)
  78. testpt = ''.join([encryptchar(char, e50) for char in message])
  79. assert testpt == sampledecrypt
  80.  
  81. key = list(' ' * 10)
  82. scorehist = []
  83. highscore = 0
  84. while True:
  85. print(''.join(key) + ' ' + str(highscore) + '\t[', end='')
  86. progress = int((highscore / len(messagetarget)) * 100)
  87. print('=' * progress, end='')
  88. print(' ' * (100 - progress), end='')
  89. print(']')
  90. if highscore == len(messagetarget):
  91. print("WOW!!!!!!!!!!!!!!!!!!!")
  92. print(key)
  93. break
  94. if len(scorehist) > 2:
  95. if scorehist[-1] == scorehist [-2]:
  96. keyind = random.randint(0,9)
  97. alphind = random.randint(0, len(alpha)-1)
  98. # print("stuck - setting key index %s to alphabet index %s" % (keyind, alphind))
  99. key[keyind] = alpha[alphind]
  100. for k in range(len(key)):
  101. keyscore = {}
  102. for a in (alpha):
  103. key[k] = a
  104. score = scoredecrypt(''.join(key))
  105. keyscore[a] = score
  106. highscore = keyscore[max(keyscore, key=keyscore.get)]
  107. key[k] = max(keyscore, key=keyscore.get)
  108. scorehist.append((key, highscore))
Add Comment
Please, Sign In to add comment