#!/usr/bin/python3 # -*- coding: utf-8 -*- # Transposition Cipher Hacker # https://www.nostarch.com/crackingcodes (BSD Licensed) import detectEnglish, transpositionDecrypt from typing import Optional def main() -> None: # You might want to copy & paste this text from the source code at # https://invpy.com/transpositionHacker.py myMessage = """AaKoosoeDe5 b5sn ma reno ora'lhlrrceey e enlh na \ indeit n uhoretrm au ieu v er Ne2 gmanw,forwnlbsya apor tE.no euarisfa\ tt e mealefedhsppmgAnlnoe(c -or)alat r lw o eb nglom,Ain one dtes il\ hetcdba. t tg eturmudg,tfl1e1 v nitiaicynhrCsaemie-sp ncgHt nie cetrg\ mnoa yc r,ieaa toesa- e a0m82e1w shcnth ekh gaecnpeutaaieetgn iodhso\ d ro hAe snrsfcegrt NCsLc b17m8aEheideikfr aBercaeu thllnrshicwsg etr\ iebruaisss d iorr.""" hackedMessage = hackTransposition(myMessage) if hackedMessage == None: print("Failed to hack encryption.") else: print("Copying hacked message to clipboard:") print(hackedMessage) def hackTransposition(message: str) -> Optional[str]: 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(1, len(message)): print("Trying key #%s..." % (key)) decryptedText = transpositionDecrypt.decryptMessage(key, message) if detectEnglish.isEnglish(decryptedText): # Ask user if this is the correct decryption. print() print("Possible encryption hack:") print("Key %s: %s" % (key, decryptedText[:100])) print() print("Enter D if done, anything else to continue hacking:") response = input("> ") if response.strip().upper().startswith("D"): return decryptedText return None if __name__ == "__main__": main()