Hacking Secret Ciphers with Python

(Ann) #1

126 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


A Transposition Cipher Decryption Program...........................................................................................................


Open a new file editor window and type out the following code in it. Save this program as
transpositionDecrypt.py.


Source Code of the Transposition Cipher Decryption 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 transpositionDecrypt.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 transpositionDecrypt.py file. You can download this file from
http://invpy.com/pyperclip.py.


Source code for transpositionDecrypt.py




  1. Transposition Cipher Decryption




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





  3. import math, pyperclip



  4. def main():

  5. myMessage = 'Cenoonommstmme oo snnio. s s c'

  6. myKey = 8



  7. plaintext = decryptMessage(myKey, myMessage)




  8. Print with a | (called "pipe" character) after it in case




  9. there are spaces at the end of the decrypted message.



  10. print(plaintext + '|')



  11. pyperclip.copy(plaintext)





  12. def decryptMessage(key, message):


  13. The transposition decrypt function will simulate the "columns" and




  14. "rows" of the grid that the plaintext is written on by using a list




  15. of strings. First, we need to calculate a few values.






  16. The number of "columns" in our transposition grid:



  17. numOfColumns = math.ceil(len(message) / key)


  18. The number of "rows" in our grid will need:



  19. numOfRows = key


  20. The number of "shaded boxes" in the last "column" of the grid:



  21. numOfShadedBoxes = (numOfColumns * numOfRows) - len(message)




  22. Each string in plaintext represents a column in the grid.



Free download pdf