Hacking Secret Ciphers with Python

(Ann) #1

234 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


key without finding the correct key. (If the program had found the correct key, then the execution
would have previously returned from the function on line 53 .)


But at this point, the hackAffine() function returns the None value to signal that it was
unsuccessful at hacking the ciphertext.


affineHacker.py



  1. If affineHacker.py is run (instead of imported as a module) call




  2. the main() function.



  3. if name == 'main':

  4. main()


Just like the other programs, we want the affineHacker.py file to be run on its own or be imported
as a module. If affineHacker.py is run as a program, then the special name variable will be
set to the string 'main' (instead of 'affineHacker'). In this case, we want to call the
main() function.


Practice Exercises, Chapter 16, Set A


Practice exercises can be found at http://invpy.com/hackingpractice 16 A.


Summary


This chapter was fairly short because it hasn’t introduced any new hacking techniques. As long as
the number of possible keys is less than a million or so, it won’t take long for our computers to
brute-force through every possible key and use isEnglish() to check if it has found the right
key.


And a lot of the code we use for the affine cipher hacker has already been written in
affineCipher.py, detectEnglish.py, cryptomath.py, and pyperclip.py. The main() function trick
is really helpful in making the code in our programs reusable.


The ** exponent operator can be used to raise a number to the power of another number. The
continue statement sends the program execution back to the beginning of the loop (instead of
waiting until the execution reaches the end of the block).


In the next chapter, we will learn a new cipher that cannot be brute-forced by our computers. The
number of possible keys is more than a trillion trillion! A single laptop couldn’t possible go
through a fraction of those keys in our life time. This makes it immune to brute-forcing. Let’s
learn about the simple substitution cipher.

Free download pdf