Enter the text you want to decode in the first line of the next cell. The output will be one big block of text that follows the uppercase convention for encrypted text.
In [1]:
cipherText = "GUVF VF GUR RAPELCGRQ GRKG LBH JNAG GB QRPBQR"
smashText = ""
for line in cipherText:
smashText = smashText + line.upper()
print smashText
Make sure to evaluate the cell above before running the cell below. The output of the cell below is the relative frequency of each letter in the cipher alphabet.
In [2]:
cipherAlphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
totalChars = float(smashText.count(""))
countList = [(letter,float(smashText.count(letter))/totalChars*100) for letter in cipherAlphabet]
sortedValues = sorted(countList, key=lambda freq: freq[1], reverse = True)
for i in sortedValues:
print i
The following is a frequency table for the english language:
Based on this information, we might expect G and R to represent e and t in some order. Based on positions in the text, we guess that G : t and R : e.
In [3]:
plaintextAlphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
plaintextAlphabet = ['A','B','C','D','E','F','t','H','I','J','K','L','M','N','O','P','Q','e','S','T','U','V','W','X','Y','Z']
In [4]:
keyDict = dict(zip(cipherAlphabet,plaintextAlphabet))
plainText = smashText
for i, j in keyDict.iteritems():
plainText = plainText.replace(i, j)
print plainText
We can make some educated guesses from here. Note that we won't be guaranteed a unique solution for our plaintext alphabet because some of the characters in the cipher alphabet are never used. But we can make enough guesses to get enough of the plaintext alphabet to decrypt the message.
In [5]:
plaintextAlphabet = ['n','o','p','D','r','s','t','u','I','w','x','y','M','a','O','c','d','e','S','T','h','i','W','X','Y','Z']
In [6]:
keyDict = dict(zip(cipherAlphabet,plaintextAlphabet))
plainText = smashText
for i, j in keyDict.iteritems():
plainText = plainText.replace(i, j)
print plainText