Hacking Secret Ciphers with Python

(Ann) #1

72 http://inventwithpython.com/hacking


Email questions to the author: [email protected]




  1. LETTERS or less than 0



  2. if num >= len(LETTERS):

  3. num = num - len(LETTERS)

  4. elif num < 0:

  5. num = num + len(LETTERS)




  6. add encrypted/decrypted number's symbol at the end of translated



  7. translated = translated + LETTERS[num]



  8. else:


  9. just add the symbol without encrypting/decrypting



  10. translated = translated + symbol




  11. print the encrypted/decrypted string to the screen



  12. print(translated)




  13. copy the encrypted/decrypted string to the clipboard



  14. pyperclip.copy(translated)


Sample Run of the Caesar Cipher Program


When you run this program, the output will look like this:


GUVF VF ZL FRPERG ZRFFNTR.


The above text is the string 'This is my secret message.' encrypted with the Caesar
cipher with key 13. The Caesar cipher program you just ran will automatically copy this
encrypted string to the clipboard so you can paste it in an email or text file. This way you can
easily take the encrypted output from the program and send it to another person.


To decrypt, just paste this text as the new value stored in the message variable on line 7. Then
change the assignment statement on line 13 to store the string 'decrypt' in the variable mode:


caesarCipher.py



  1. the string to be encrypted/decrypted



  2. message = 'GUVF VF ZL FRPERG ZRFFNTR.'




  3. the encryption/decryption key



  4. key = 13




  5. tells the program to encrypt or decrypt



  6. mode = 'decrypt' # set to 'encrypt' or 'decrypt'


When you run the program now, the output will look like this:

Free download pdf