Hacking Secret Ciphers with Python

(Ann) #1
Chapter 16 – Hacking the Affine Cipher 231

...is the same as:


len(affineCipher.SYMBOLS) * len(affineCipher.SYMBOLS)

We multiply this because there are at most len(affineCipher.SYMBOLS) possible integers
for Key A and len(affineCipher.SYMBOLS) possible integers for Key B. To get the
entire range of possible keys, we multiply these values together.


Line 34 calls the getKeyParts() function that we made in affineCipher.py to get the Key A
part of the key we are testing. Remember that the return value of this function call is a tuple of
two integers (one for Key A and one for Key B). Since hackAffine() only needs Key A, the
[0] after the function call works on the return value to evaluate to just the first integer in the
returned tuple.


That is, affineCipher.getKeyParts(key)[0] will evaluate to (for example), the tuple
(42, 22)[0], which will then evaluate to 42. This is how we can get just the Key A part of
the return value. The Key B part (that is, the second value in the returned tuple) is just ignored
because we don’t need Key B to calculate if Key A is valid.


The continue Statement


The continue statement is simply the continue keyword by itself. A continue statement
is found inside the block of a while or for loop. When a continue statement is executed, the
program execution immediately jumps to the start of the loop for the next iteration.


This is exactly the same thing that happens when the program execution reaches the end of the
loop’s block. But a continue statement makes the program execution jump back to the start of
the loop early.


Try typing the following into the interactive shell:





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




Free download pdf