Skip to main content

A modern, fully tested Enigma machine simulator in Python.

Project description

Python Tests License codecov

๐Ÿ” Enigma Machine Simulator (M3 โ†’ M4)

A clean, modular, historically accurate simulation of the German Enigma M3 and Kriegsmarine M4 cipher machines โ€” implemented in modern Python, with full test coverage and historically faithful mechanics.

This project emphasizes:

  • correctness
  • mechanical fidelity
  • clean architecture
  • testability
  • extensibility

It is both a learning tool and a professional portfolio project demonstrating engineering discipline and historical accuracy.

๐Ÿ“š Documentation

๐Ÿš€ Quick Start

from enigma.core.machine import EnigmaMachine
from enigma.core.rotor import Rotor
from enigma.core.reflector import Reflector
from enigma.core.plugboard import Plugboard
from enigma.data.rotors import ROTOR_I, ROTOR_II, ROTOR_III
from enigma.data.reflectors import REFLECTOR_B

# Create components
rotors = [
    Rotor(*ROTOR_I, position="A"),
    Rotor(*ROTOR_II, position="A"),
    Rotor(*ROTOR_III, position="A")
]
reflector = Reflector(REFLECTOR_B)
plugboard = Plugboard()

# Create machine and encrypt
machine = EnigmaMachine(rotors, reflector, plugboard)
encrypted = machine.encode_letter("H")

๐Ÿ“ฆ Installation

Install from PyPI:

pip install enigma-m4

Or install from source:

git clone https://github.com/wrogistefan/enigma_m4.git
cd enigma_m4
pip install -e .

โœจ Features

๐Ÿง  Core Capabilities

  • Full M3 support (rotors Iโ€“VIII, reflectors A/B/C)
  • Full M4 support (IIIโ€“IIโ€“I + Greek rotor)
  • Greek rotors: Beta, Gamma
  • Thin reflectors: Thin B, Thin C
  • Historically accurate stepping and doubleโ€‘stepping
  • Ring settings (Ringstellung)
  • Rotor positions (Grundstellung)
  • Plugboard (Steckerbrett) with validation
  • Full reversibility (Enigma property)

๐Ÿ”Œ Historical Plugboard

Supports all historically used formats:

{"A": "B"}                     # dict
[("A", "B"), ("C", "D")]       # list of pairs
"PO ML IU KZ"                  # Kriegsmarine format
"A-B C-D"                      # dash format

Up to 10 pairs, matching real Enigma constraints.


๐Ÿงฉ Architecture Overview

The machine is composed of independent, testable components:

Plugboard
   โ†“
Right Rotor โ†’ Middle Rotor โ†’ Left Rotor โ†’ Greek Rotor (M4)
   โ†“             โ†“              โ†“             โ†“
                     Reflector
   โ†‘             โ†‘              โ†‘             โ†‘
Right Rotor โ† Middle Rotor โ† Left Rotor โ† Greek Rotor
   โ†‘
Plugboard

๐Ÿ”ง Components

๐Ÿ”Œ Plugboard

  • Bidirectional letter swapping
  • Full validation
  • Historical 10โ€‘pair limit
  • API: swap(char)

โš™ Rotors

  • Forward & backward signal path
  • Ring setting
  • Rotor position
  • Notch logic
  • step() rotation
  • Includes:
    • I, II, III, IV, V
    • VI, VII, VIII (Kriegsmarine)
    • Beta, Gamma (M4 static rotors)

๐Ÿชž Reflectors

  • Involutive mapping
  • No fixed points
  • Includes:
    • A, B, C
    • Thin B, Thin C (M4)

๐Ÿ–ฅ EnigmaMachine

  • Correct stepping logic (M3 + M4)
  • Doubleโ€‘stepping implemented
  • Full signal path implemented
  • Plugboard integration
  • Reversible encryption

๐Ÿ“ Project Structure

enigma_m4/
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ docs/
โ”‚   โ”œโ”€โ”€ USER_MANUAL.md
โ”‚   โ””โ”€โ”€ API_REFERENCE.md
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ enigma/
โ”‚       โ”œโ”€โ”€ __init__.py
โ”‚       โ”œโ”€โ”€ core/
โ”‚       โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚       โ”‚   โ”œโ”€โ”€ rotor.py
โ”‚       โ”‚   โ”œโ”€โ”€ reflector.py
โ”‚       โ”‚   โ”œโ”€โ”€ plugboard.py
โ”‚       โ”‚   โ””โ”€โ”€ machine.py
โ”‚       โ”œโ”€โ”€ data/
โ”‚       โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚       โ”‚   โ”œโ”€โ”€ rotors.py
โ”‚       โ”‚   โ””โ”€โ”€ reflectors.py
โ”‚       โ””โ”€โ”€ utils/
โ”‚           โ”œโ”€โ”€ __init__.py
โ”‚           โ”œโ”€โ”€ cli/
โ”‚           โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚           โ”‚   โ””โ”€โ”€ main.py
โ”‚           โ””โ”€โ”€ gui/
โ”‚               โ”œโ”€โ”€ __init__.py
โ”‚               โ””โ”€โ”€ app.py
โ””โ”€โ”€ tests/
    โ”œโ”€โ”€ __init__.py
    โ”œโ”€โ”€ test_rotor.py
    โ”œโ”€โ”€ test_reflector.py
    โ”œโ”€โ”€ test_plugboard.py
    โ”œโ”€โ”€ test_machine.py
    โ”œโ”€โ”€ test_rotors.py
    โ”œโ”€โ”€ test_reflectors.py
    โ”œโ”€โ”€ test_integration.py
    โ”œโ”€โ”€ test_integration_m4.py
    โ”œโ”€โ”€ test_integration_m4_historical.py
    โ””โ”€โ”€ test_plugboard.py

๐Ÿ“– Documentation

For detailed usage instructions and API reference:


๐Ÿงช Testing

Run all tests:

pytest -q

The test suite includes:

  • unit tests for rotors, reflectors, plugboard
  • M3 integration tests
  • M4 integration tests
  • historical canonical tests:
    • Beta + Thin B โ†’ A โ†’ B
    • Gamma + Thin C โ†’ A โ†’ P
  • reversibility tests
  • ring setting tests
  • plugboard tests
  • Kriegsmarineโ€‘style configuration tests

๐Ÿ—บ Roadmap

โœ… Completed (v0.4.0)

  • Full M3 implementation
  • Full M4 implementation
  • Greek rotors (Beta/Gamma)
  • Thin reflectors (Thin B/C)
  • Historical plugboard formats
  • Full stepping logic
  • Comprehensive test suite

๐Ÿ”œ Next

  • Add historical daily key sheets
  • Add real Uโ€‘boat message examples
  • Add CLI interface
  • Add web demo
  • Add visualization of rotor stepping

๐Ÿ“œ License

This project is licensed under the MIT License.


๐Ÿ‘ค About the Author

ลukasz Perek โ€” Python developer focused on clean architecture, cryptography, and historically inspired engineering.

Based in Syracuse, Sicily, transitioning into fullโ€‘time software engineering and AIโ€‘driven development.

Specializes in:

  • Python (OOP, CLI tools, packaging, testing)
  • Clean, modular architecture
  • Cryptographic systems & historical computing
  • CI/CD (GitHub Actions, linting, coverage)
  • Documentation & developer experience

This Enigma simulator is part of his public portfolio โ€” a demonstration of engineering discipline, historical accuracy, and modern Python design.

If you find this project interesting or useful, feel free to โญ the repository.


๐Ÿ”— Connect

GitHub LinkedIn

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

enigma_m4-0.4.0.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

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

enigma_m4-0.4.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file enigma_m4-0.4.0.tar.gz.

File metadata

  • Download URL: enigma_m4-0.4.0.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for enigma_m4-0.4.0.tar.gz
Algorithm Hash digest
SHA256 06e56f142bca957426eb219e9d278680f02feebe3880c9454642c48d9390eed0
MD5 d847ce9d188aabcd723e4d9a51a229dd
BLAKE2b-256 205655b899615f3701ddb18f8c13f2a5fd244e26a7612cea8a64d57f55f7ec13

See more details on using hashes here.

File details

Details for the file enigma_m4-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: enigma_m4-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for enigma_m4-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca3044fe32cdf5229cf05ca2ce89b407c918d2db5eacb1e5ccdaa4f67c382992
MD5 b95404e777af690e74d9f76fb96b558d
BLAKE2b-256 8b002467b3b3d0e6e9fb515045d1c1a717088cd6699bc41203a38afbbde73f85

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