Hacking Secret Ciphers with Python

(Ann) #1

68 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


evaluates to True, the program execution moves inside the block again.
Step 10 Line 9 translated + message[i] evaluates '.d'. The string value '.d'
is stored in the translated variable.


Step 11 Line 10 (^) i - 1 evaluates to 46. The integer 46 is stored in the i variable.
Step 12 Line 8 (^) The while statement rechecks the condition. Since i >= 0 evaluates to
True, the program execution will move inside the block again.
Step 13
to
Step 149
(^) ...The lines of the code continue to loop. We fast-forward to when i is set to
0 and translated is set to ' .daed era meht fo owt fi
,terces a peek nac eerh'...
Step 150 Line 8 (^) The while loop’s condition is checked, and 0 >= 0 evaluates to True.
Step 151 Line 9 (^) translated + message[i] evaluates to '.daed era meht fo
owt fi ,terces a peek nac eerhT'. This string is stored in the
translated variable.
Step 152 Line 10 i - 1 evaluates to 0 - 1 , which evaluates to - 1. - 1 is stored in the i
variable.
Step 153 Line 8 (^) The while loop’s condition is i >= 0, which evaluates to - 1 >= 0,
which evaluates to False. Because the condition is now False, the
program execution skips the following block of code and goes to line 12.
Step 154 Line 12 translated evaluates to the string value '.daed era meht fo
owt fi ,terces a peek nac eerhT'. The print() function is
called and this string is passed, making it appear on the screen.
There are no more lines after line 12, so the program terminates.


Using input() In Our Programs


The programs in this book are all designed so that the strings that are being encrypted or
decrypted are typed directly into the source code. You could also modify the assignment
statements so that they call the input() function. You can pass a string to the input()
function to appear as a prompt for the user to type in the string to encrypt. For example, if you
change line 4 in reverseCipher.py to this:


reverseCipher.py


  1. message = input('Enter message: ')


Then when you run the program, it will print the prompt to the screen and wait for the user to
type in the message and press Enter. The message that the user types in will be the string value
that is stored in the message variable:


Enter message: Hello world!
!dlrow olleH

Free download pdf