Chapter 13 – Hacking the Transposition Cipher 193
transpositionHacker.py
- if detectEnglish.isEnglish(decryptedText):
Check with user to see if the decrypted key has been found.
- print()
- print('Possible encryption hack:')
- print('Key %s: %s' % (key, decryptedText[:100]))
- print()
- print('Enter D for done, or just press Enter to continue
hacking:') - response = input('> ')
The decrypted output in decryptedText will most likely only be English if the correct key
was used (otherwise, it will appear to be random garbage). The string in decryptedText is
passed to the detectEnglish.isEnglish() function we wrote in the last chapter.
But just because detectEnglish.isEnglish() returns True (making the program
execution enter the block following the if statement on line 34) doesn’t mean the program has
found the correct key. It could be a “false positive”. To be sure, line 38 prints out the first 100
characters of the decryptedText string (by using the slice decryptedText[:100]) on
the screen for the user to look at.
The program pauses when line 41 executes, waiting for the user to type something in either D on
nothing before pressing Enter. This input is stored as a string in response.
The strip() String Method
The strip() string method returns a version of the string that has any
whitespace at the beginning and end of the string stripped out. Try typing in
the following into the interactive shell:
' Hello'.strip()
'Hello'
'Hello '.strip()
'Hello'
' Hello World '.strip()
'Hello World'
'Hello x'.strip()
'Hello x'
The strip() method can also have a string argument passed to it that tells the method which
characters should be removed from the start and end of the string instead of removing whitespace.