Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- def decrypt(ciphertext, n, m):
- ciphertext_int = [ord(i)-ord('a') for i in ciphertext]
- plaintext = ''
- for i in range(len(ciphertext_int)):
- plain_code = (ciphertext_int[i] - n - m*i) % 26
- plaintext += chr(plain_code + ord('a'))
- return plaintext
- def getfreqs(text):
- freqs = np.zeros(26)
- for c in text:
- freqs[ord(c) - ord('a')] = freqs[ord(c) - ord('a')] + 1
- freqs = freqs / len(text)
- return freqs
- # From wikipedia
- 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,\
- 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]
- ciphertext = 'ryurbqatxhwhxryufxppwtjkvzwvwijkgpebudvirjqxxrhdbvoztgctnbbcdwlszfscmffkqphxstkundhfotxlqnkcrjxvoxislbzmvznwfirpfelkdre\
- rjtxmrwvvdmrapjjqbhlsomjkgazfdxdcnfgmzfctqeexnowdisral'
- maxdot = 0
- key_n = 0
- key_m = 0
- for n in range(26):
- for m in range(26):
- dotprod = np.dot(english_freqs, getfreqs(decrypt(ciphertext, n, m).lower()))
- if(dotprod > maxdot):
- maxdot = dotprod
- key_n = n
- key_m = m
- print("%d,%d had the highest dot product of %f" % (key_n, key_m, maxdot))
- print("The plaintext is most likely %s" % decrypt(ciphertext, key_n, key_m))
Add Comment
Please, Sign In to add comment