Hacking Secret Ciphers with Python

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

For our example decryption, plaintext will be ['Common s', 'ense is ', 'not so
c', 'ommon.'], so ''.join(plaintext) will evaluate to 'Common sense is not
so common.'.


transpositionDecrypt.py



  1. If transpositionDecrypt.py is run (instead of imported as a module) call




  2. the main() function.



  3. if name == 'main':

  4. main()


The first line that our program runs after importing modules and executing the def statements is
the if statement on line 54. Just like in the transposition encryption program, we check if this
program has been run (instead of imported by a different program) by checking if the special
name variable is set to the string value 'main'. If so, we execute the main()
function.


Practice Exercises, Chapter 9, Set C


Practice exercises can be found at http://invpy.com/hackingpractice9C.


Summary


That’s it for the decryption program. Most of the program is in the decryptMessage()
function. We can see that our programs can encrypt and decrypt the message “Common sense is
not so common.” with the key 8. But we should try several other messages and keys to see that a
message that is encrypted and then decrypted will result in the same thing as the original
message. Otherwise, we know that either the encryption code or the decryption code doesn’t
work.


We could start changing the key and message variables in our transpositionEncrypt.py and
transpositionDecrypt.py and then running them to see if it works. But instead, let’s automate this
by writing a program to test our program.

Free download pdf