Skip to content

Metahemeralism/pynigma

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Enigma Machine Simulator

image

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.

Further Reading

Enigma Machine — Computerphile

Background

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.

Features

  • 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

Project Structure

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

Getting Started

Requirements: Python 3.10+ (tkinter is included in the standard library)

Install test dependencies:

pip install -r requirements.txt

Run the GUI

python interfaces/gui.py

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

Enigma Machine GUI

Run tests

pytest tests/

Usage

In the GUI:

  1. Select a rotor type (I, II, III) and start position for each of the three rotor slots
  2. Select a reflector (UKW-A, UKW-B, or UKW-C)
  3. Optionally enter plugboard pairs (e.g. AB CD EF)
  4. Type your message and click Encode / Decode

Enigma is symmetric — encoding a ciphertext with the same settings produces the original plaintext.

Programmatic usage

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)

About

Python replica of the WW2 Enigma Machine used by the Germans

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages