Skip to main content

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

Metadata

Release files for enigma-m4 0.4.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 enigma-m4 0.4.0
File Size Uploaded
enigma_m4-0.4.0.tar.gz 17.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for enigma-m4 0.4.0
File Interpreter ABI Platform
enigma_m4-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 28.7 kB

Release files / enigma_m4-0.4.0.tar.gz

Download URL enigma_m4-0.4.0.tar.gz
Size 17.5 kB
Tags Source
SHA-256 checksum
How to use checksums
06e56f142bca957426eb219e9d278680f02feebe3880c9454642c48d9390eed0
BLAKE2b-256 checksum
How to use checksums
205655b899615f3701ddb18f8c13f2a5fd244e26a7612cea8a64d57f55f7ec13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / enigma_m4-0.4.0-py3-none-any.whl

Download URL enigma_m4-0.4.0-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ca3044fe32cdf5229cf05ca2ce89b407c918d2db5eacb1e5ccdaa4f67c382992
BLAKE2b-256 checksum
How to use checksums
8b002467b3b3d0e6e9fb515045d1c1a717088cd6699bc41203a38afbbde73f85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release history Release notifications | RSS feed

This release

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