Hacking Secret Ciphers with Python

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


  1. plaintext = [''] * numOfColumns




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




  3. character in the encrypted message will go.



  4. col = 0

  5. row = 0



  6. for symbol in message:

  7. plaintext[col] += symbol

  8. col += 1 # point to next column




  9. If there are no more columns OR we're at a shaded box, go back to




  10. the first column and the next row.



  11. if (col == numOfColumns) or (col == numOfColumns - 1 and row >=
    numOfRows - numOfShadedBoxes):

  12. col = 0

  13. row += 1



  14. return ''.join(plaintext)






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




  16. the main() function.



  17. if name == 'main':

  18. main()


When you run the above program, it produces this output:


Common sense is not so common.|


If you want to decrypt a different message, or use a different key, change the value assigned to
the myMessage and myKey variables on lines 5 and 6.


How the Program Works..........................................................................................................................................


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



Free download pdf