CC-Enigma Machine (CryptoCrackers v1)
An object-oriented Python implementation of the classic electro-mechanical German Enigma encryption machine.
Project Overview
The CC-Enigma Machine is a highly accurate software simulation of the physical Enigma rotor cipher machine used during World War II. Built entirely in Python using clean object-oriented design principles, this project models the complete electrical signal path—including the plugboard, multiple shifting rotors with mechanical notches, and a symmetric reflector.
Due to the mechanical design of the physical machine, Enigma encryption is entirely symmetrical: if you pass plaintext through the machine under a specific key configuration, it outputs ciphertext. If you reset the rotors to the exact same starting configuration and input the ciphertext, it deciphers back to the original plaintext. This emulator models that behavior flawlessly.
Technical Architecture & The Signal Path
To encrypt a single letter, the program emulates the continuous electrical path of the mechanical machine, passing indices through four distinct physical modules:
Key Components
-
The Plugboard (
Plugboardclass): Emulates the physical patch cords on the front panel of the machine. It maps specific character pairs statically (e.g., swapping 'A' with 'Z' and 'B' with 'Y') before and after the rotors process the signal. -
The Rotors (
Rotorclass): Implements the mechanical rotation states. Every keypress cycles the first rotor by 1 position. The mechanical notches are modeled using a mechanicalturnoverstate—triggering the adjacent rotor to cycle when reaching notch thresholds (including the rare historical physical "double-stepping" anomaly). -
The Reflector (
Reflectorclass): Acts as a reciprocal mirror. By reflecting the electrical signal backwards, it guarantees that a letter can never encrypt to itself, which mathematically enables the symmetrical encryption/decryption properties.
Key Python Implementation
Below is the core of the cryptographic pipeline inside the Enigma orchestrator. This block manages the physical rotor stepping rotation logic before passing the character index bidirectionally through the virtual circuitry:
# Emulating the mechanical rotation and electrical signal path
def encode(self, c):
c = c.upper()
if not c.isalpha():
return c
# Step the fast rotor first
self.rotors[0].rotate()
# Double-stepping anomaly: physical quirk of the middle rotor
if self.rotors[1].base[0] in self.rotors[1].notch:
self.rotors[1].rotate()
# Cascade the stepping rotation down the rotor bank
for i in range(len(self.rotors) - 1):
if self.rotors[i].turnover:
self.rotors[i].turnover = False
self.rotors[i + 1].rotate()
# Bidirectional Signal Routing:
index = self.plugboard.forward(c) # Forward through Plugboard
for r in self.rotors:
index = r.forward(index) # Forward through Rotors
index = self.reflector.forward(index) # Reflected Backwards
for r in reversed(self.rotors):
index = r.reverse(index) # Reversed back through Rotors
c = self.plugboard.reverse(index) # Reversed back through Plugboard
return c
Key Engineering Takeaways
- Hardware vs. Software Modeling: Realized the unique challenges of translating analog mechanical limitations (like gear-shifting turnovers and physical copper wire connections) into highly structured, performant OOP code.
- State Management: Developed a profound respect for state tracking. Ensuring every state (turnovers, base indexes, and ring offsets) could be reset predictably allowed the decryption routines to work with 100% mathematical consistency.