Hacking Secret Ciphers with Python

(Ann) #1

232 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


This is pretty obvious. The for loop will loop through the range object, and the value in i
becomes each integer between 0 and 4. Also on each iteration, the print('Hello!')
function call will display “Hello!” on the screen.


Try typing in this code, which adds a continue statement before the print('Hello!')
line:





for i in range( 3 ):
... print(i)
... continue
... print('Hello!')
...
0
1
2





Notice that “Hello!” never appears, because the continue statement causes the program
execution to jump back to the start of the for loop for the next iteration. So the execution never
reaches the print('Hello!') line.


A continue statement is often put inside an if statement’s block so that execution will
continue at the beginning of the loop based on some condition.


affineHacker.py
35. if cryptomath.gcd(keyA, len(affineCipher.SYMBOLS)) != 1:
36. continue


With the Key A integer stored in the variable keyA, line 35 uses the gcd() function in our
cryptomath module to determine if Key A is not relatively prime with the symbol set size.
Remember, two numbers are relatively prime if their GCD (greatest common divisor) is one.


If Key A and the symbol set size are not relatively prime, then the condition on line 35 is True
and the continue statement on line 36 is executed. This will cause the program execution to
jump back to the start of the loop for the next iteration. This way, the program will skip line 38’s
call to decryptMessage() if the key is invalid, and continue to the next key.


affineHacker.py


  1. decryptedText = affineCipher.decryptMessage(key, message)

  2. if not SILENT_MODE:

  3. print('Tried Key %s... (%s)' % (key, decryptedText[:40]))

Free download pdf