Skip to main content

Substitution cipher solver using pattern matching and constraint propagation

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cryptograms-0.1.0.tar.gz (24.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cryptograms-0.1.0-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file cryptograms-0.1.0.tar.gz.

File metadata

  • Download URL: cryptograms-0.1.0.tar.gz
  • Upload date:
  • Size: 24.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for cryptograms-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5fd4f1fcc7cea1e1e606d85c7b34a1af097fe9e14084be4838bfb49b1f986de0
MD5 0905c6072cb8be02affcdd14a4baab61
BLAKE2b-256 706c1cbde4368fcabf0e245f7903b6ed4b743d8a2292eebcc157e772e1e32aef

See more details on using hashes here.

File details

Details for the file cryptograms-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cryptograms-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for cryptograms-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9aaabe6809b518901d26ce4492b472222df43f732bf603b7b96182e71253540b
MD5 60604aaad0b7f54eaa56b9f92d16cbdd
BLAKE2b-256 d3ebb0f357c4b97ebef3168f4564d7bcd8f0b7d80debe35a4e21df8dbf43492a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page