Skip to main content

cryptograms

A Python package for solving simple substitution ciphers using pattern matching and constraint propagation.

Installation

pip install cryptograms

Usage

Python API

from cryptograms import solve_cryptogram, encrypt_cryptogram

# Solve a cipher
solution = solve_cryptogram("Ebiil tloia")
print(solution.plaintext)    # Hello world
print(solution.confidence)   # 0.95

# Encrypt plaintext (useful for generating puzzles)
ciphertext = encrypt_cryptogram("Hello world")
print(ciphertext)            # e.g. Ebiil tloia

Command line

# Decrypt — accepts a string or a file path
cryptograms decrypt "Ebiil tloia"
cryptograms decrypt puzzle.txt

# Encrypt
cryptograms encrypt "Hello world"
cryptograms encrypt "Hello world" --seed 42   # reproducible output

How it works

The solver uses word-pattern matching and constraint propagation rather than frequency analysis:

  1. Each cipher word is encoded as a numeric pattern — "KHOOR" → "12334"
  2. Candidate plaintext words are looked up by pattern from a ~200k-word frequency-weighted dictionary
  3. LetterConstraints tracks which plaintext letters each cipher letter can still map to, and propagates locks globally when a mapping is confirmed
  4. Candidates and constraints are narrowed iteratively until every cipher word resolves to one plaintext word
  5. When stuck, the solver uses NLTK Brown corpus bigram/trigram frequencies to pick the most contextually likely candidate and continues

Data

Word frequencies from wordfreq. Bigram and trigram context from the NLTK Brown corpus, downloaded automatically on first use.

Web interface

A self-hostable web interface is available in the repository. Run it with:

pip install cryptograms[web]
uvicorn cryptograms.api:app

Metadata

Release files for cryptograms 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cryptograms 0.1.0
File Size Uploaded
cryptograms-0.1.0.tar.gz 24.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cryptograms 0.1.0
File Interpreter ABI Platform
cryptograms-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 43.5 kB

Release files / cryptograms-0.1.0.tar.gz

Download URL cryptograms-0.1.0.tar.gz
Size 24.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5fd4f1fcc7cea1e1e606d85c7b34a1af097fe9e14084be4838bfb49b1f986de0
BLAKE2b-256 checksum
How to use checksums
706c1cbde4368fcabf0e245f7903b6ed4b743d8a2292eebcc157e772e1e32aef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / cryptograms-0.1.0-py3-none-any.whl

Download URL cryptograms-0.1.0-py3-none-any.whl
Size 19.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9aaabe6809b518901d26ce4492b472222df43f732bf603b7b96182e71253540b
BLAKE2b-256 checksum
How to use checksums
d3ebb0f357c4b97ebef3168f4564d7bcd8f0b7d80debe35a4e21df8dbf43492a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page