daily pastebin goal
1%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2.  
  3. def decrypt(ciphertext, n, m):
  4.     ciphertext_int = [ord(i)-ord('a') for i in ciphertext]
  5.     plaintext = ''
  6.     for i in range(len(ciphertext_int)):
  7.         plain_code = (ciphertext_int[i] - n - m*i) % 26
  8.         plaintext += chr(plain_code + ord('a'))
  9.     return plaintext
  10.  
  11. def getfreqs(text):
  12.     freqs = np.zeros(26)
  13.     for c in text:
  14.         freqs[ord(c) - ord('a')] = freqs[ord(c) - ord('a')] + 1
  15.     freqs = freqs / len(text)
  16.     return freqs
  17.  
  18. # From wikipedia
  19. english_freqs = [0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228, 0.02015, 0.06094, 0.06996, 0.00153, 0.00772, 0.04025, 0.02406,\
  20.  0.06749, 0.07507, 0.01929, 0.00095, 0.05987, 0.06327, 0.09056, 0.02758, 0.00978, 0.02360, 0.00150, 0.01974, 0.00074]
  21.  
  22. ciphertext = 'ryurbqatxhwhxryufxppwtjkvzwvwijkgpebudvirjqxxrhdbvoztgctnbbcdwlszfscmffkqphxstkundhfotxlqnkcrjxvoxislbzmvznwfirpfelkdre\
  23. rjtxmrwvvdmrapjjqbhlsomjkgazfdxdcnfgmzfctqeexnowdisral'
  24.  
  25. maxdot = 0
  26. key_n = 0
  27. key_m = 0
  28.  
  29. for n in range(26):
  30.     for m in range(26):
  31.         dotprod = np.dot(english_freqs, getfreqs(decrypt(ciphertext, n, m).lower()))
  32.         if(dotprod > maxdot):
  33.             maxdot = dotprod
  34.             key_n = n
  35.             key_m = m
  36.  
  37. print("%d,%d had the highest dot product of %f" % (key_n, key_m, maxdot))
  38. print("The plaintext is most likely %s" % decrypt(ciphertext, key_n, key_m))
RAW Paste Data
Top