Hacking Secret Ciphers with Python

(Ann) #1

102 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


The rest of the programs in this book have a function named main() which is called at the start
of program. The reason is explained at the end of this chapter, but for now just know that the
main() function in the programs in this book are always called soon after the programs are run.


On lines 7 and 8, the variables myMessage and myKey will store the plaintext message to
encrypt and the key used to do the encryption.


transpositionEncrypt.py


  1. ciphertext = encryptMessage(myKey, myMessage)


The code that does the actual encrypting will be put into a function we define on line 21 named
encryptMessage(). This function will take two arguments: an integer value for the key and a
string value for the message to encrypt. When passing multiple arguments to a function call,
separate the arguments with a comma.


The return value of encryptMessage() will be a string value of the encrypted ciphertext.
(The code in this function is explained next.) This string will be stored in a variable named
ciphertext.


transpositionEncrypt.py



  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)


The ciphertext message is printed to the screen on line 15 and copied to the clipboard on line 18.
The program prints a | character (called the “pipe” character) at the end of the message so that the
user can see any empty space characters at the end of the ciphertext.


Line 18 is the last line of the main() function. After it executes, the program execution will
return to the line after the line that called it. The call to main() is on line 45 and is the last line
in the program, so after execution returns from main() the program will exit.


Parameters


transpositionEncrypt.py


  1. def encryptMessage(key, message):

Free download pdf