A Python simulation of the German Wehrmacht Enigma machine used during World War II, featuring historically accurate rotor stepping, plugboard swapping, and reflector routing — with both a graphical and programmatic interface.
Enigma Machine — Computerphile
The Enigma machine was a cipher device used by the German military to encrypt communications. Its interlocking system of rotors, a plugboard, and a reflector produced a polyalphabetic substitution cipher that was exceptionally difficult to break. The Allied effort to crack it — led by Alan Turing and the team at Bletchley Park — is widely credited as a turning point in the war and a foundation of modern computer science.
This project simulates the Wehrmacht/Luftwaffe Enigma I, including the double-stepping anomaly and full plugboard support.
- Rotors — historically accurate wiring for rotors I, II, and III with correct notch positions and double-stepping
- Reflectors — support for UKW-A, UKW-B, and UKW-C
- Plugboard — up to 10 letter-pair swaps (Steckerbrett)
- GUI — tkinter-based graphical interface with rotor, reflector, and plugboard configuration
- Config-driven — all historical wirings loaded from
config/JSON files
enigma/
├── core/
│ ├── rotor.py # Rotor with stepping, forward/backward encoding
│ ├── reflector.py # Reflector with wiring validation
│ ├── plugboard.py # Plugboard (Steckerbrett) with pair validation
│ └── machine.py # EnigmaMachine assembly and encoding pipeline
├── config/
│ ├── rotors.json # Historical rotor wirings and notch positions
│ └── reflectors.json # Historical reflector wirings (UKW-A/B/C)
├── interfaces/
│ └── gui.py # Graphical interface (tkinter)
├── tests/
│ ├── test_rotor.py
│ └── test_machine.py
├── utils/
│ └── __init__.py # Config loading helpers
└── enigma.py # Entry point / scratch runner
Requirements: Python 3.10+ (tkinter is included in the standard library)
Install test dependencies:
pip install -r requirements.txtpython interfaces/gui.pyThis will open the Enigma Machine GUI shown below. Use the dropdowns to select a rotor type (I, II, III) and start position for each of the three rotor slots, choose a reflector (UKW-A, UKW-B, or UKW-C), and optionally enter plugboard pairs (e.g. AB CD EF). Type your message in the input field and click Encode / Decode to encrypt or decrypt. Since Enigma is symmetric, running the same ciphertext through with identical settings will recover the original plaintext.
pytest tests/In the GUI:
- Select a rotor type (I, II, III) and start position for each of the three rotor slots
- Select a reflector (UKW-A, UKW-B, or UKW-C)
- Optionally enter plugboard pairs (e.g.
AB CD EF) - Type your message and click Encode / Decode
Enigma is symmetric — encoding a ciphertext with the same settings produces the original plaintext.
from core.machine import EnigmaMachine
from core.rotor import Rotor
from core.reflector import Reflector
from core.plugboard import Plugboard
rotors = [
Rotor("I", "EKMFLGDQVZNTOWYHXUSPAIBRCJ", "Q"),
Rotor("II", "AJDKSIRUXBLHWTMCQGZNPYFVOE", "E"),
Rotor("III", "BDFHJLCPRTXVZNYEIWGAKMUSQO", "V"),
]
machine = EnigmaMachine(rotors, Reflector(), Plugboard(["AB", "CD"]))
machine.set_positions("AAA")
ciphertext = "".join(machine.encode_message("HELLO"))
print(ciphertext)