Overview

Introduction

Py-Enigma is a Python 3 library for simulating the Enigma machines used by the German armed forces (Wehrmacht) during World War II. Py-Enigma is historically accurate, meaning it can interoperate with actual Wehrmacht Enigma machines. In other words, Py-Enigma can decrypt coded messages created with an actual Enigma, and it can encrypt messages that an actual Enigma can decode.

It is hoped that this library will be useful to Enigma enthusiasts, historians, and students interested in cryptography.

Py-Enigma strives to be Pythonic, easy to use, and comes with both unit tests and documentation. Py-Enigma is a library for building applications for encrypting and decrypting Enigma messages. However, it also ships with a simple command-line application that can encrypt & decrypt messages for scripting and experimentation.

Scope

Currently, Py-Enigma simulates the Wehrmacht Enigma machines. This includes the 3 and 4 rotor machines used by the German Army (Heer), Air Force (Luftwaffe), and Navy (Kriegsmarine). Simulation of other Enigma models, including the various commercial, railroad, foreign market, and Abhwer (Military Intelligence) models may come later if there is enough interest and data available.

Quick Example

This example shows how the library can be used to decode a message using the procedure employed by the German army

from enigma.machine import EnigmaMachine

# setup machine according to specs from a daily key sheet:

machine = EnigmaMachine.from_key_sheet(
       rotors='II IV V',
       reflector='B',
       ring_settings=[1, 20, 11],
       plugboard_settings='AV BS CG DL FU HZ IN KM OW RX')

# set machine initial starting position
machine.set_display('WXC')

# decrypt the message key
msg_key = machine.process_text('KCH')

# decrypt the cipher text with the unencrypted message key
machine.set_display(msg_key)

ciphertext = 'NIBLFMYMLLUFWCASCSSNVHAZ'
plaintext = machine.process_text(ciphertext)

print(plaintext)

This program prints:

THEXRUSSIANSXAREXCOMINGX

Py-Enigma also includes a command-line application for processing messages. Assuming you have a proper key file that contains the same initial settings as the code above, the above example can be performed on the command-line:

$ pyenigma.py --key-file=keys.txt --start=WXC --text='KCH'
BLA
$ pyenigma.py --key-file=keys.txt --start=BLA --text='NIBLFMYMLLUFWCASCSSNVHAZ'
THEXRUSSIANSXAREXCOMINGX

The format of the key file can be found in Key file format.

Requirements

Py-Enigma is written in Python, specifically Python 3.2. It has no other requirements or dependencies.

Installation

Py-Enigma is available on the Python Package Index (PyPI). You can install it using pip:

$ pip install py-enigma             # install
$ pip install --upgrade py-enigma   # upgrade

You may also download a tarball or .zip file of the latest code using the “get source” link on the Py-Enigma Bitbucket page. Alternatively if you use Mercurial, you can clone the repository with the following command:

$ hg clone https://bitbucket.org/bgneal/enigma

If you did not use pip, you can install with this command:

$ python setup.py install

Support & Source

All support takes place at the Py-Enigma Bitbucket page. Please enter any feature requests or bugs into the issue tracker.

You may also clone the Mercurial source code repository:

$ hg clone https://bitbucket.org/bgneal/enigma

Acknowledgements & References

This software would not have been possible without the thorough and detailed descriptions of the Enigma machine on Dirk Rijmenants’ incredible Cipher Machines and Cryptology website. In particular, his Technical Details of the Enigma Machine page was a gold mine of information.

Dirk has also written an Enigma simulator in Visual Basic. Although I did not look at his source code, I did use his simulator to check the operation of Py-Enigma.

I would also like to recommend the photos and video at Dr. Thomas B. Perera’s Enigma Museum.

Another good website is The Enigma and the Bombe by Graham Ellsbury.

A nice video which shows the basic components and operation of the Enigma Machine is on YouTube: Nadia Baker & Enigma demo.