Skip to main content

Substitution Cipher Solver

Algorithm for solving simple, monoalphabetic substitution ciphers

This is Python implementation of the algorithm for solving simple, monoalphabetic substitution ciphers described in the paper “A Fast Method for the Cryptanalysis of Substitution Ciphers” by Thomas Jakobsen. The main difference from the paper is that random key swaps are used instead of a deterministic series of swaps since it yields better results, but the original method is included and can be used as an option.

Installing

pip install cipher_solver

API

class SimpleSolver:
    """Simple substitution cipher solver."""

    def __init__(self, ciphertext):
        """Create new solver.

        Creates a new cipher solver from an initial ciphertext.
        """

    def solve(self, method="random"):
        """Solve the cipher.

        Run the solver and save the resulting decryption key.
        """

    def plaintext(self):
        """Return a plaintext using the current decryption key."""

    def reset(self):
        """Reset the solver to its initial state.

        Set the decryption key to its initial state, effectively starting over.
        """

See the documentation for full description of methods and their parameters.

Requirements

  • numpy

Usage

from cipher_solver.simple import SimpleSolver

# Solve a cipher.
s = SimpleSolver("U kn kgmhksz tkm exmpb xt Gxesxe.")
s.solve()
print(s.plaintext())  # "I am already far north of London."

s.reset()  # Discard current solution to start over.
s.solve()
print(s.plaintext())  # We have an alternative solution now.

print(s.decryption_key())  # "goaskbihxvrldepfwntmzqjucy"

# Solve using the original key swap method instead.
d = SimpleSolver("U kn kgmhksz tkm exmpb xt Gxesxe.", method="deterministic")
d.solve()
print(s.plaintext())

Note, however, that the above ciphertext is too short to give any meaningful results. A length of at least a few hundred letters is preferred to solve a cipher. See below for an example using an included sample text.

CLI

A simple command-line interface is included. To solve a cipher, put it into a text file and run:

cipher_solver <path_to_ciphertext_file>

Example:

cipher_solver texts/26_char_key/ciphertexts/ciphertext_frankenstein_sample.txt

Since the algorithm involves hill climbing and randomness you might sometimes end up with complete gibberish, just run the script again and the next result should be better.

Running tests

make test

Checking coverage

make coverage

(Requires the coverage package.)

Generating documentation

make docs

(Requires the pdoc3 package.)

Metadata

Release files for cipher-solver 1.0.2

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

Source distribution (sdist)

Source distribution for cipher-solver 1.0.2
File Size Uploaded
cipher_solver-1.0.2.tar.gz 12.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cipher-solver 1.0.2
File Interpreter ABI Platform
cipher_solver-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 24.0 kB

Release files / cipher_solver-1.0.2.tar.gz

Download URL cipher_solver-1.0.2.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4c8dcf0ef96fab593394f8965705f4810d938d6a0e6537c2af20251c76781606
BLAKE2b-256 checksum
How to use checksums
6cbd39bb025690030ff5c9318ab4cef76aea3138d3211e95da7286ce438194bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / cipher_solver-1.0.2-py3-none-any.whl

Download URL cipher_solver-1.0.2-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b6419461356465c2f67ff8383156055f91bd6eead1020b684b1a201279b54cea
BLAKE2b-256 checksum
How to use checksums
357ea549abe27dd8e696c8dd239b62dd2e18e87a9225a1353802c37c702cf7c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

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