Hacking Secret Ciphers with Python

(Ann) #1
Chapter 8 – The Transposition Cipher, Encrypting 99







  1. Print the encrypted string in ciphertext to the screen, with




  2. a | (called "pipe" character) after it in case there are spaces at




  3. the end of the encrypted message.



  4. print(ciphertext + '|')




  5. Copy the encrypted string in ciphertext to the clipboard.



  6. pyperclip.copy(ciphertext)





  7. def encryptMessage(key, message):


  8. Each string in ciphertext represents a column in the grid.



  9. ciphertext = [''] * key




  10. Loop through each column in ciphertext.



  11. for col in range(key):

  12. pointer = col




  13. Keep looping until pointer goes past the length of the message.



  14. while pointer < len(message):


  15. Place the character at pointer in message at the end of the




  16. current column in the ciphertext list.



  17. ciphertext[col] += message[pointer]




  18. move pointer over



  19. pointer += key




  20. Convert the ciphertext list into a single string value and return it.



  21. return ''.join(ciphertext)






  22. If transpositionEncrypt.py is run (instead of imported as a module) call




  23. the main() function.



  24. if name == 'main':

  25. main()


Sample Run of the Transposition Cipher Encryption Program


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


Cenoonommstmme oo snnio. s s c|


This ciphertext (without the pipe character at the end) is also copied to the clipboard, so you can
paste it into an email to someone. If you want to encrypt a different message or use a different

Free download pdf