Hacking Secret Ciphers with Python

(Ann) #1
Chapter 11 – Encrypting and Decrypting Files 165







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




  2. call the main() function.




  3. if name == 'main':




  4. main()




Afterwards, we print some more messages to the user telling them that the process is done and
what the name of the written file is. Line 48 is the last line of the main() function.


Lines 53 and 54 (which get executed after the def statement on line 6 is executed) will call the
main() function if this program is being run instead of being imported. (This is explained in
Chapter 8’s “The Special name Variable” section.)


Practice Exercises, Chapter 11, Set A


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


Summary


Congratulations! There wasn’t much to this new program aside from the open(), write(),
read(), and close() functions, but this lets us encrypt text files on our hard drive that are
megabytes or gigabytes in size. It doesn’t take much new code because all of the implementation
for the cipher has already been written. We can extend our programs (such as adding file reading
and writing capabilities) by importing their functions for use in new programs. This greatly
increases our ability to use computers to encrypt information.


There are too many possible keys to simply brute-force and examine the output of a message
encrypted with the transposition cipher. But if we can write a program that recognizes English (as
opposed to strings of gibberish), we can have the computer examine the output of thousands of
decryption attempts and determine which key can successfully decrypt a message to English.

Free download pdf