A TypeScript CLI tool for solving Playfair cipher text using simulated annealing.
This project includes tools to:
- Precompute log probabilities from N-gram frequency files.
- Run a solver that attempts to break Playfair ciphers using a heuristic search.
- Build custom Playfair keys via preprocessing utilities.
- 🔹 Modular design with separate components:
Precompute— Generate log probabilities from 4-gram datasetsSimulatedAnnealing— Heuristic solver for deciphering ciphertextPlayfair— Encryption/decryption implementation for Playfair cipherPreprocess— Utility functions for key sanitization and transformationUtil— Functional helpers likepipe
- 🔹 Supports custom configurations:
- Adjustable temperature, decay rate, and iteration steps for annealing.
- Flexible input/output filenames.
git clone https://github.com/hannoobz/playfair-cipher.git
cd playfair-ciphernpm installnpm run buildThis compiles TypeScript files into the dist/ folder.
The project provides a single CLI entry point: app.js.
Run it through npm run dev or directly with node dist/app.js.
Before solving, you must precompute a log probability file from an N-gram frequency dataset.
npm run precompute -- --input=4grams.txtTION 13168375
NTHE 11234972
THER 10218035
THAT 8980536
OFTH 8132597
FTHE 8100836
THES 7717675
This example is taken from PracticalCryptography
http://practicalcryptography.com/cryptanalysis/text-characterisation/quadgrams/
Creates a new file named:
logprob-4grams.txt
Once you have logprob-4grams.txt, run the solver to decrypt a ciphertext file.
npm run solve --logprob=logprob-4grams.txt --cipher=cipher.txt --keys=keys.txt --guidedKey=QWERTYUIOPASDFGHKLZXCVBNM| Option | Type | Required | Default | Description |
|---|---|---|---|---|
--logprob |
string | ✅ Yes | - | Precomputed log probability file |
--cipher |
string | ✅ Yes | - | Ciphertext file to decrypt |
--keys |
string | ✅ Yes | - | Output file where best keys will be saved |
--guidedKey |
string | ❌ No | Randomized string | Initial Playfair key |
--temp |
number | ❌ No | 30 | Starting temperature for annealing |
--rate |
number | ❌ No | 0.01 | Temperature decay rate |
--steps |
number | ❌ No | 2000 | Steps per temperature iteration |
npm run precompute -- --input=4grams.txtOutput:
logprob-4grams.txt
Create a cipher.txt file containing the Playfair cipher text you want to decrypt:
BMODZBXDNABEKUDMUIXMMOUVIF
npm run solve -- \
--logprob=logprob-4grams.txt \
--cipher=cipher.txt \
--keys=keys.txt \
--guidedKey=QWERTYUIOPASDFGHKLZXCVBNM \
--temp=10 \
--rate=0.5 \
--steps=100The program will log its progress and append the best key found to keys.txt.
M. Hanief Fatkhan Nashrullah
Email: 13522100@std.stei.itb.ac.id
This is just an implementation in TypeScript based on the article: Practical Cryptography - Cryptanalysis of the Playfair Cipher