/enigma

A Python CLI simulating the Enigma Code Machine

Primary LanguagePython

Enigma

The purpose of this repository is to create a digital replica of the Enigma Code Machine of World War II fame. In particular, it is a replica of the Enigma M4, which was used exclusively by the U-Boat division of the Kriegsmarine. It has been made famous in pop culture to some extent by movies like The Imitation Game and U-571.

The repository is split into three files:

  • enigma.py
  • rotors.py
  • test.py

The rotors.py file contains the parent class Rotor and all of its methods. The children classes (RotorI, RotorII, etc.) inherit directly from it, each one representing the actual wiring of the original rotors. Special thanks to cryptomuseum.com for their invaluable resource on the wiring of the Enigma's rotors. Apart from differences in wiring and the location of the "turnover" point, all rotors behave much the same.

The test.py file contains a suite of unit tests on a miniaturized Rotor object (with a 6-letter alphabet) to test every attribute and method of the Rotor.

The enigma.py file contains the Enigma class. The reflector and entrywheel are fixed attributes of the class, which utilize a 26-letter all-caps alphabet. It is certainly possible to make those attributes imports as well like the rotors and expand the plaintext alphabet to include whitespace, special characters, etc., but it is worth noting that Enigma's use of a reflector coerces that the alphabet must have an even number of characters.

When enigma.py is run directly, it launches a command-line program that prompts the user for all the initial settings needed for the enigma and a message to encrypt. The code can be made a more robust to protect against edge cases, but works fine so long as the user is somewhat familiar with Enigma.

Future improvements to this repository may include adding a plugboard functionality to Enigma. The plugboard simply scrambled plaintext before it was passed to the entrywheel. I omitted it purely for the sake of time but it would make for a more authentic user experience.

The Enigma was a reversible code, meaning that if two users had the same initial settings on their machines, the ciphertext generated by user A could be typed directly into user B's machine to yield the plaintext. This is also true in this program. Try encrypting a message with one set of initial settings, save the ciphertext, and then rerun the program with the same settings but "encrypting" ciphertext instead of another message. The output should be your original message. Remember that Enigma only supports the 26-letter alphabet, so any numbers, whitespace, or special characters will be stripped out of your message before encryption.

Disclaimer

The Nazis lost the war for a variety of reasons, including the fact that the Allies were reading their supposedly secure messages. Do not use this repository to encrypt stuff that actually matters.