Hacking Secret Ciphers with Python

(Ann) #1

100 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


key, change the value assigned to the myMessage and myKey variables on lines 7 and 8. Then
run the program again.


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


transpositionEncrypt.py



  1. Transposition Cipher Encryption




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





  3. import pyperclip


The transposition cipher program, like the Caesar cipher program, will copy the encrypted text to
the clipboard. So first we will import the pyperclip module so it can call
pyperclip.copy().


Creating Your Own Functions with def Statements


transpositionEncrypt.py


  1. def main():

  2. myMessage = 'Common sense is not so common.'

  3. myKey = 8


A function (like print()) is a sort of mini-program in your program. When the function is
called, the execution moves to the code inside that function and then returns to the line after the
function call. You can create your own functions with a def statement like the one on line 6.


The def statement on line 6 isn't a call to a function named main(). Instead, the def statement


means we are creating, or defining, a new function named main() that we can call later in our
program. When the execution reaches the def statement Python will define this function. We can
then call it the same way we call other functions. When we call this function, the execution
moves inside of the block of code following the def statement.


Open a new file editor window and type the following code into it:


Source code for helloFunction.py



  1. def hello():

  2. print('Hello!')

  3. total = 42 + 1

  4. print('42 plus 1 is %s' % (total))

  5. print('Start!')

  6. hello()

  7. print('Call it again.')

  8. hello()

Free download pdf