Hacking Secret Ciphers with Python

(Ann) #1
Chapter 9 – The Transposition Cipher, Decrypting 131

string for each column of the grid. Using list replication, we can multiply a list of one blank string
by numOfColumns to make a list of several blank strings.


(Remember that each function call has its own local scope. The plaintext in
decryptMessage() exists in a different local scope than the plaintext variable in
main(), so they are two different variables that just happen to have the same name.)


Remember that the grid for our 'Cenoonommstmme oo snnio. s s c' example looks
like this:


C e n o
o n o m
m s t m
m e (s) o
o (s) s n
n i o.
(s) s (s)
s (s) c

The plaintext variable will have a list of strings. Each string in the list is a single column of
this grid. For this decryption, we want plaintext to end up with this value:





plaintext = ['Common s', 'ense is ', 'not so c', 'ommon.']
plaintext[0]
'Common s'





That way, we can join all the list’s strings together to get the 'Common sense is not so
common.' string value to return.


transpositionDecrypt.py



  1. The col and row variables point to where in the grid the next




  2. character in the encrypted message will go.



  3. col = 0

  4. row = 0



  5. for symbol in message:


The col and row variables will track the column and row where the next character in message
should go. We will start these variables at 0. Line 39 will start a for loop that iterates over the
characters in the message string. Inside this loop the code will adjust the col and row
variables so that we concatenate symbol to the correct string in the plaintext list.

Free download pdf