Hacking Secret Ciphers with Python

(Ann) #1

124 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


Unlike the Caesar cipher, the decryption process for the transposition cipher is very different
from the encryption process. In this chapter we will create a separate program,
transpositionDecrypt.py, to handle decryption.


Decrypting with the Transposition Cipher on Paper


Let’s pretend we send the ciphertext “Cenoonommstmme oo snnio. s s c” to a friend (and she
already knows that the secret key is 8). The first step for her to decrypt the ciphertext is to
calculate how many boxes she needs to draw. To find this amount, divide the length of the
ciphertext message by the key and round up. The length of our ciphertext is 30 characters (exactly
the same as the plaintext) and the key is 8. So calculate 30 divided by 8 to get 3.75.


3.75 rounds up to 4. This means we want to draw a grid of boxes with 4 columns (the number we
just calculated) and 8 rows (the key). It will look like this:


(Note that if the length divided by the key was a whole number, like in 30 / 5 = 6.0, then 6.0
would not “round up” to 7.)


The second thing we need to calculate is how many boxes on the rightmost column to shade in.
Take the total number of boxes (32) and subtract the length of the ciphertext (30). 32 – 30 = 2, so
shade in the bottom 2 boxes on the rightmost column:

Free download pdf