In World War II the German Military used a rotor cipher machine commonly known as “Enigma” to communicate in secret. Because of the seemingly random way it encrypted messages it was an extremely effective piece of equipment during its heyday. Enigma went through many iterations each of which drove interesting developments in mathematics and computer technology.

For a while I’ve been fascinated by the Engima Machine and have wanted to learn more about it. Having decided the best way to approach the daunting subject was to just build one I chose Microsoft Excel as the initial medium to reconstruct Enigma. I’ve created this post to go with it as a kind of Enigma primer, covering with broad strokes the mechanisms that make Enigma work.

#### Ciphers in General

Ciphers generally work by scrambling letters in some pseudo random fashion. Take a substitution cipher for example where the alphabet is shifted 3 to left. A becomes E, B becomes F etc. This would’ve worked pretty well for Julius Caesar but is very easy for anyone with a good grasp of the alphabet or probability to crack. If “Z” is the most common letter in a piece of encrypted text, then you can be pretty sure it’s actually an “E” in disguise. You don’t actually need to know how the letters were shifted, you can just work it out with probabilities.

On the surface Enigma is no different since it substitutes one letter with another. The difference is that Enigma uses a set of stepping rotors to change the substitution process with every keypress. So pressing the same key twice in a row would give two different encrpyted letters. Since the substitution process keeps changing no letter is more likely to appear than any other. This makes it mostly impossible to crack Enigma without first knowing both how the machine works and also know how the operator has set up the machine.

So… How does the machine work?

#### Enigma Components

When a key on the Enigma keyboard was pressed, electrical current would pass through the maze of wiring within the Enigma until reaching the output key. Before reaching that letter though, electrical current has to first pass through the following components of the Enigma. Each acts to further complicate the substitution process, making Enigma harder to crack

**Steckerbrett:**This is a plugboard where operators could swap letters. Connecting X and A on the plugboard would turn X’s into A’s and A’s into X’s.**Eintrittswalze (ETW):**This is the entry wheel. The A button connects to the 1st contact, B to the second etc.

Each of the 3 wheels installed in the Enigma step at different intervals. The rightmost wheel steps with every keypress. The other wheels step less frequently according to the notch on the wheel to their right. When reached, that notch causes the next wheel to the left to take one step. So the middle wheel steps every 26 steps of the rightmost wheel. The leftmost wheel steps every 26 steps of the middle wheel. Because of this the left most wheel hardly ever steps at all. This was improved in later Enigma models.**Rotors:**Each rotor has 26 contacts (representing letters of the alphabet) on each side. The contacts on each side of the wheel are connected from one side to the other in some random fashion. To confuse things further, there were 5 different rotors of which 3 were present in the machine at any one time. Oh, and did I mention they rotate?**Umkehrwalze (UKW):**This is a reflector which redirects the electrical current back into the rotors. Having reached the leftmost rotor, the UKW sends current back in the other direction.

Okay so.. Through the Stecker, into the entry wheel, through 3 rotors (right to left) which turn with each keypress, into the UKW, back into through the rotors again (left to right), into the entry wheel and back through the Stecker before finally lighting up a lamp to display the encrypted letter. Easy right? Let’s go through an example of what happens when a key is pressed…

#### Excel Enigma Workbook and an Example Message

The Enigma Macro Workbook can be used to simulate the Enigma I. It uses a lot of lookups, conditional formatting and a little bit of XY Scatter magic. The instructions are a bit lacking, but should be fairly self explanatory. Using it you pick the wheels you want to use, which index to start them at and the message to encode by entering values in the light green boxes. My favourite thing about this is that it lets you see the path taken through Enigma by each letter. In the example below let’s assume our Enigma machine is configured such that that rotors IV, II and I are installed in that order from left to right and are set to the 6th, 24th and 10th index respectively.

If we were to press the letter H on the keyboard, the first wheel will take one step forward (increasing the index to 11). Then, starting from the right with the letter “H” the path created through each component within Engima can be followed until eventually reaching the letter “U”. Watch the animation below as it encodes a “HELLOWORLD” message and notice that each letter takes a very different path.

Now suppose someone sends us the message “UINTTPGASJ”. All we have to do is configure the machine the same way it was configured to start with (wheels IV, II and I set to indexes 6, 24 and 10) then encode that message. It’s just a matter of knowing how the machine is setup. When configured this way the first keypress “H” will always turn into “U” and “U” will always turn into “H”.

#### Mathematical Complexity

So, UINTTPGASJ huh? Assuming you knew how Enigma machines work then surely you could just brute force your way through the different Enigma configurations to decipher this message right? It actually turns out this is actually a pretty difficult task. Between the steckerbrett, the choice of wheels, ring setting and indexing on each one it turns out there are 107,458,491,300,000,000,000,000 (1.07 x 10^{23}) ways of configuring the Enigma I. This is an impressive number but despite this was cracked repeatedly throughout the War. This wasn’t really due to a weakness of Enigma, but rather the laziness of some German operators who tended to reuse the same keys and let their codebooks get captured.

#### Wrapping up the Enigma

My original intent was simply to learn how Enigma worked by recreating it. Having done that I feel like I can wrap this up here. There are a few spin off projects I’ll be keeping in mind for future projects however.- Recreating Engima in something other than Excel. Java might be good so I can host it, though I’ve never touched Java before. It could be a good way to learn what can and can’t be done in either googledocs or R too.
- Retracing the steps of Polish, British and American cryptanalysts and their efforts to crack Enigma would be very interesting.
- Creating a physical version of Enigma. Perhaps a miniaturised pocket Enigma, or maybe even something digital?

I found creating an Excel Enigma an extremely enjoyable activity and highly recommend anyone interested in learning Enigma to try it. It can be quite complex in some regards, but that makes it a good way to test the boundaries of a language/program you already know well.

#### Disclaimer

I don’t claim to be any kind of expert on either Enigma or ciphers. This work is based solely on reading and my interpretation of the references mentioned below and hence might not be completely accurate, though should be pretty close (have tested it against the Enigma Simulator below) and is definitely useful at least as an intro to the concepts behind Enigma.

#### References & Further Reading

- Crypto Museum For being one of the most incredible sources of Enigma info out there. If you want to learn more about the Enigma, this is the place for it. Images of Enigma machines in this post also provided courtesy of these guys too.
- Sabaton: For having heaps of excellent WWII inspired power metal
- Enigma Simulator: A downloadable Enigma simulator. Contains a few different types of Enigma and was very useful for error checking my Excel Enigma.
- Solving the Enigma: History of the Cryptanalytic Bombe (pdf): Very detailed piece on how Enigma (and attempts to crack it) evolved through the War.
- The Cryptographic Mathematics of Enigma: Useful info on the mathematical complexity of Enigma.
- Building an Enigma Machine: Resource for how to build your own Enigma. Useful if I ever decide to make that pocket Enigma…
- Famous Codes & Ciphers Through History: Good write up of cryptography throughout history