Chapter 13 – Hacking the Transposition Cipher 191
The ciphertext to be hacked is stored in the myMessage variable. Line 9 has a string value that
begins and ends with triple quotes. These strings do not have to have literal single and double
quotes escaped inside of them. Triple quote strings are also called multi-line strings, because they
can also contain actual newlines within them. Try typing the following into the interactive shell:
spam = """Dear Alice,
Why did you dress up my hamster in doll clothing?
I look at Mr. Fuzz and think, "I know this was Alice's doing."
Sincerely,
Bob"""
print(spam)
Dear Alice,
Why did you dress up my hamster in doll clothing?
I look at Mr. Fuzz and think, "I know this was Alice's doing."
Sincerely,
Bob
Notice that this string value can span over multiple lines. Everything after the opening triple
quotes will be interpreted as part of the string until it reaches triple quotes ending it. Multi-line
strings can either use three double quote characters or three single quote characters.
Multi-line strings are useful for putting very large strings into the source code for a program,
which is why it is used on line 9 to store the ciphertext to be broken.
Back to the Code
transpositionHacker.py
- hackedMessage = hackTransposition(myMessage)
The ciphertext hacking code exists inside the hackTransposition() function. This function
takes one string argument: the encrypted ciphertext message to be broken. If the function can
hack the ciphertext, it returns a string of the decrypted text. Otherwise, it returns the None value.
This value is stored in the hackedMessage variable.
transpositionHacker.py
- if hackedMessage == None:
- print('Failed to hack encryption.')
If None was stored in hackedMessage, the program prints that it was unable to break the
encryption on the message.