Skip to main content

Algorithm for solving simple, monoalphabetic substitution ciphers

Project description

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.)

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

cipher_solver-1.0.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

cipher_solver-1.0.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file cipher_solver-1.0.1.tar.gz.

File metadata

  • Download URL: cipher_solver-1.0.1.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.6

File hashes

Hashes for cipher_solver-1.0.1.tar.gz
Algorithm Hash digest
SHA256 c01628083a69039e2daaa5cee9b6387cbfa8a020f16eb6aeff93b126b0acf4f3
MD5 1e08107a4d945fb121b7026e350c167a
BLAKE2b-256 69ce6a596a6b3dc4694385a627a3bda65ada337c29e49af4a6cad16811e7b5ca

See more details on using hashes here.

File details

Details for the file cipher_solver-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: cipher_solver-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.6

File hashes

Hashes for cipher_solver-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d4a48e433f319ba2bfb1361ce52d2bb30447ed7048ec543bcc9fdbd6e9238855
MD5 f50941eda760f40995fa0f916b62e715
BLAKE2b-256 44dd0602ee22643315d481ee8624bdaf81e4e1283d68e3958720fc811b4572ac

See more details on using hashes here.

Supported by

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