An automated cryptanalysis engine in pure Python 3 implementing letter frequency analysis, n-gram scoring, multi-restart stochastic hill-climbing optimization, and Kasiski / Index of Coincidence / Chi-squared statistical attacks to break classical mono-alphabetic and poly-alphabetic (Vigenère) ciphers.
mono-alphabetic.txt: your mono-substitution ciphertextpoly-alphabetic.txt: Vigenere/poly-alphabetic ciphertextmono_decoder.py: Python script for mono frequency analysis and refinementpoly_decoder.py: Python script for Vigenere key recovery and decryption
- Counts ciphertext letter frequencies
- Optionally prints digraph/trigraph and double-letter frequency tables
- Builds an initial substitution using standard English frequency order
- Runs a lightweight hill-climbing swap search to improve readability
- Prints both initial and refined plaintext guesses
python mono_decoder.py --input mono-alphabetic.txt --show-ngrams --show-map --output mono-guess.txtUseful options:
--iterations 50000for deeper search--seed 7to try a different random route--restarts 20for more hill-climb restarts--top 10to print only top 10 letter frequencies--show-ngramsto include digraph/trigraph/double-letter tables--progress-every 1000to print more frequent hill-climb progress updates--early-stop 0to disable early stopping across restarts--quietto suppress restart/progress log noise
Suggested run profiles:
- Quick check:
python mono_decoder.py --iterations 8000 --restarts 6 --quiet - Deeper pass:
python mono_decoder.py --iterations 80000 --restarts 25 --progress-every 2000 --output mono-guess.txt - Full diagnostic:
python mono_decoder.py --show-ngrams --show-map --top 26
The poly decoder uses:
- Kasiski repeated trigram distance factor votes (key-length hints)
- Index of Coincidence ranking for key lengths
- Per-column Caesar chi-squared matching to recover each key letter
Run auto key recovery:
python poly_decoder.py --input poly-alphabetic.txt --show-analysis --output poly-guess.txtUseful options:
--min-keylen 2 --max-keylen 25to widen key-length search--top-lens 10to evaluate more candidate lengths--key LEMONif you already know the key--show-analysisto print IC ranking and Kasiski vote hints--output poly-guess.txtto save decrypted plaintext to a file
Suggested run profiles:
- Quick auto-recovery:
python poly_decoder.py --top-lens 6 - Wider key search:
python poly_decoder.py --min-keylen 2 --max-keylen 30 --top-lens 12 --show-analysis - Known key decrypt:
python poly_decoder.py --key LEMON --output poly-guess.txt
This engine accompanies the cryptographic research and cryptanalysis coursework published on Code and Cypher:
- Cryptographic Controls & Kerckhoffs's Principle: Security Models and Kerckhoffs's Principle
- Breaking Alphabetic Ciphers: Breaking Alphabetic Ciphers
- Public-Key & Modern Cryptography: Public Key Cryptography