Hacking Secret Ciphers with Python

(Ann) #1

98 http://inventwithpython.com/hacking


Email questions to the author: [email protected]



  1. Shade in the unused boxes in the last row.

  2. Starting from the top left and going down, write out the characters. When you get to the
    bottom of the column, move to the next column to the right. Skip any shaded boxes. This
    will be the ciphertext.


Practice Exercises, Chapter 8, Set A


Practice exercises can be found at http://invpy.com/hackingpractice 8 A.


A Transposition Cipher Encryption Program


Encrypting with paper and pencil involves a lot of work and it’s easy to make mistakes. Let’s
look at a program that can implement transposition cipher encryption (a decryption program will
be demonstrated later in this chapter).


Using the computer program has a slight problem, however. If the ciphertext has space characters
at the end, then it is impossible to see them since a space is just empty... well, space. To fix this,
the program adds a | character at the end of the ciphertext. (The | character is called the “pipe”
character and is above the Enter key on your keyboard.) For example:


Hello| # There are no spaces at the end of the message.
Hello | # There is one space at the end of the message.
Hello | # There are two spaces at the end of the message.


Source Code of the Transposition Cipher Encryption Program


Open a new file editor window by clicking on File ► New Window. Type in the following code
into the file editor, and then save it as transpositionEncrypt.py. Press F5 to run the program. Note
that first you will need to download the pyperclip.py module and place this file in the same
directory as the transpositionEncrypt.py file. You can download this file from
http://invpy.com/pyperclip.py.


Source code for transpositionEncrypt.py




  1. Transposition Cipher Encryption




  2. http://inventwithpython.com/hacking (BSD Licensed)





  3. import pyperclip



  4. def main():

  5. myMessage = 'Common sense is not so common.'

  6. myKey = 8



  7. ciphertext = encryptMessage(myKey, myMessage)

Free download pdf