Skip to content

Repository files navigation

Cryptanalysis & Cipher Decoding Engine

License: MIT Python 3.8+ Optimization: Stochastic Hill-Climbing Analysis: Kasiski / Chi-Squared

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.

Files

  • mono-alphabetic.txt: your mono-substitution ciphertext
  • poly-alphabetic.txt: Vigenere/poly-alphabetic ciphertext
  • mono_decoder.py: Python script for mono frequency analysis and refinement
  • poly_decoder.py: Python script for Vigenere key recovery and decryption

What the script does

  1. Counts ciphertext letter frequencies
  2. Optionally prints digraph/trigraph and double-letter frequency tables
  3. Builds an initial substitution using standard English frequency order
  4. Runs a lightweight hill-climbing swap search to improve readability
  5. Prints both initial and refined plaintext guesses

Run

python mono_decoder.py --input mono-alphabetic.txt --show-ngrams --show-map --output mono-guess.txt

Useful options:

  • --iterations 50000 for deeper search
  • --seed 7 to try a different random route
  • --restarts 20 for more hill-climb restarts
  • --top 10 to print only top 10 letter frequencies
  • --show-ngrams to include digraph/trigraph/double-letter tables
  • --progress-every 1000 to print more frequent hill-climb progress updates
  • --early-stop 0 to disable early stopping across restarts
  • --quiet to 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

Next Step: Vigenere / Poly-Alphabetic Decoder

The poly decoder uses:

  1. Kasiski repeated trigram distance factor votes (key-length hints)
  2. Index of Coincidence ranking for key lengths
  3. 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.txt

Useful options:

  • --min-keylen 2 --max-keylen 25 to widen key-length search
  • --top-lens 10 to evaluate more candidate lengths
  • --key LEMON if you already know the key
  • --show-analysis to print IC ranking and Kasiski vote hints
  • --output poly-guess.txt to 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

Research & Academic Context

This engine accompanies the cryptographic research and cryptanalysis coursework published on Code and Cypher:

About

Automated cryptanalysis engine in pure Python. Utilizes multi-restart stochastic hill-climbing optimization to crack mono-alphabetic substitution ciphers and Kasiski/IC/Chi-Squared slicing to dismantle poly-alphabetic ciphers.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages