Chapter 16 – Hacking the Affine Cipher 227
- def main():
You might want to copy & paste this text from the source code at
- myMessage = """U&'<3dJ^Gjx'-3^MS'Sj0jxuj'G3'%j'<mMMjS'g{GjMMg9j{G'g"'gG
'<3^MS'Sj<jguj'm'P^dm{'g{G3'%jMgjug{9'GPmG'gG'-m0'P^dm{LU'5&Mm{'_^xg{9"""
- hackedMessage = hackAffine(myMessage)
- if hackedMessage != None:
The plaintext is displayed on the screen. For the convenience of
the user, we copy the text of the code to the clipboard.
- print('Copying hacked message to clipboard:')
- print(hackedMessage)
- pyperclip.copy(hackedMessage)
- else:
- print('Failed to hack encryption.')
- def hackAffine(message):
- print('Hacking...')
Python programs can be stopped at any time by pressing Ctrl-C (on
Windows) or Ctrl-D (on Mac and Linux)
- print('(Press Ctrl-C or Ctrl-D to quit at any time.)')
brute-force by looping through every possible key
- for key in range(len(affineCipher.SYMBOLS) ** 2):
- keyA = affineCipher.getKeyParts(key)[0]
- if cryptomath.gcd(keyA, len(affineCipher.SYMBOLS)) != 1:
- continue
- decryptedText = affineCipher.decryptMessage(key, message)
- if not SILENT_MODE:
- print('Tried Key %s... (%s)' % (key, decryptedText[:40]))
- if detectEnglish.isEnglish(decryptedText):
Check with the user if the decrypted key has been found.
- print()
- print('Possible encryption hack:')
- print('Key: %s' % (key))
- print('Decrypted message: ' + decryptedText[:200])
- print()
- print('Enter D for done, or just press Enter to continue
hacking:')
- response = input('> ')