It was the technological weapon designed to keep German communications secret during World War II – but the secret within the secret was that British Intelligence had cracked the Enigma cipher machine, giving them invaluable knowledge of Hitler’s plans. It’s often said this ‘Ultra secret’ shortened the war by many months.
Enigma may be simple by today’s cryptography standards, but it was the efforts to crack the more complex Lorenz SZ40/42 machines that led to the construction of the first all-electronic computer called ‘Colossus’ in 1943.
While it was shrouded in secrecy during the war, Enigma’s secrets are well known today – enough for us to turn an Arduino Uno microcontroller into an Enigma Cipher Machine.
More specifically, our project this month is a fully functional copy of the German Army or Wehrmacht Enigma, implementing the three-of-five rotor system (along with the software implementation of the actual rotor wiring), user-settable rotor start positions, plugboard and rotor ring settings or ‘ringstellung’. I’ve created the software (sketch) from scratch, based on technical information available at the UK Museum of Computing at Bletchley Park. Bottom line, any message encoded by a real Wehrmacht Enigma machine can be decoded by our little black box (with the right settings, of course).
The hardware design is simple enough – a standard Arduino Uno R2 or R3 microcontroller board, a 1602 LCD shield plus a six-pin mini-DIN panel socket, a couple of switches and a PS/2 keyboard.
What started out as a simple project to show you how you can connect up a PS/2 keyboard to an Arduino as an input device quickly evolved beyond that. In context, you use the keyboard to set up the Enigma’s settings and type in either your ‘plain text’ or encoded message. The Arduino instantly encodes or decodes each letter, displaying it underneath your typed input.
How the real Enigma works
Electrically, the Enigma machine is little more than switches, wire, light bulbs and a battery, but its brilliance lies in its ever-moving rotors that continually change the encoding key. They ensure that pressing the same keyboard key lights up a different encoded letter each time. The Wehrmacht machines feature three rotors from a choice of five, all wired differently; navy machines use four from eight. Each rotor consists of two discs with 26 pin and contact terminals on the outer side of each disc and wiring between the terminals on the inside.
It’s this wiring that creates the first layer of encryption. But as further rotors are added, the connections between rotors and their incremental rotation on each key press create over 17,500 possible circuit combinations. These combinations are expanded by the 60 different ways of choosing three rotors from a possible five and swapping the rotor order. But it was the addition of the plugboard with its 150million million ways of wiring letter pairs that pushed up the total number of combinations to over 15 million million million. The plugboard allows you to swap letters on the keyboard, so for example, swapping ‘E’ and ‘T’ would effectively change the keyboard from the standard German keys ‘QWERTZ’ to ‘QWTREZ’.
The key to Enigma’s design however was the last built-in rotor called the ‘reflector’ – it completed the electrical circuit, enabling the machine to encode and decode letters. If you started with the same rotor selection/position/ring points, typing an ‘A’ would give you an ‘E’ for example, but typing an ‘E’ would also give you the original ‘A’ – and that’s why Enigma was so potent (this was also one of its weaknesses). In the end, its compact design and ease-of-use meant thousands of Enigma machines were used by all branches of the German armed forces.
There’s a basic principle of computing called the Principle of Equivalence that states that any task done by software can be performed by hardware and any operation carried out by hardware can also be achieved in software.
In my Arduino source code or ‘sketch’, we’ve turned the rotors into software elements called ‘arrays’, which are variables with multiple elements, each referenced by an index number – a bit like units in a block of flats. The block is the array and each flat is an element in that array. The array elements are arranged so that the index becomes the common link between the rotor arrays. This allows us to follow the ‘wiring’ and keep track of the correct key combination based on the rotor start position and ring setting.
The hardware design
The 1602 LCD shield replaces the Enigma’s lamp board – in normal operation, it displays the current rotor position (P) and the rotor order (R) on the left. On the right, the last seven characters are shown – your input (I) on the top and the encoded/decoded output (O) underneath.
The Enigma mechanical keyboard is replaced by a PS/2 keyboard here, made possible by the freely-available PS2Keyboard library. All we need for electronics is a six-pin mini-DIN panel socket wired directly to the Arduino. The PS/2 keyboard operates off a 5VDC supply rail and has two data lines – one for clock signal and another for keyboard data. These connect to pins D2 and D11, respectively, on the Arduino. The library enables us to read the data from the keyboard and know which key is pressed.
The momentary normally-open (NO) pushbutton switch provides a reset button, pulling the Arduino’s reset pin to ground (0VDC), restarting the Enigma software.
We’ve built our Arduino Enigma Cipher Machine into a small jiffy box from Jaycar Electronics, using 3mm machine screws and nuts through the mounting holes on the 1602 LCD shield to hold it in place. We’ve also added a 2.1mm DC panel socket to the box chassis and soldered it to the Arduino’s built-in DC socket. This allows us easy access to external power but also to incorporate a simple SPDT (single-pole double-throw) power switch. Power can be supplied by any 7-12VDC power brick with tip-positive voltage polarity. (Make sure you wire it up with the right polarity to the board.)
How to use it
Using the Arduino Enigma is similar to a real Enigma machine. You start by setting the rotor order – remember, you have a choice of five, so to select rotors I, III and IV in that order, left to right, you enter ‘134’ on the keyboard. You can use the left and right arrow keys to change individual rotor settings, the Enter key to move to the next setting.
After that, you set the initial rotor start position – for example, ‘APC’ sets rotor I to position ‘A’, rotor III to ‘P’ and rotor IV to ‘C’. The next setting is the rotor ring setting or ‘ringstellung’, which rotates the internal ring wiring to a different letter. Again, you enter a three-letter group for the three rotors. Finally, you set the plugboard – like the real Enigma, you can enter up to ten two-letter groups, each pair being the letters swapped (remember, don’t repeat any letters).
At each point, you can choose to simply press the Enter key to select the default settings. Doing this for every option yields rotors I, II and III, start position ‘AAA’, ring position ‘AAA’ and no plugboard pairs. (The plugboard is optional).
Now all you do is start typing in your plain text or encoded message – the last seven characters you type appear on the top line of the display, the encoded/decoded character underneath. Despite its seemingly slow 16MHz/8-bit processor, the Arduino Enigma is still incredibly quick and can encode/decode faster than you can type.
To mimic the real thing, once you start typing your message, you cannot go backwards since each key press advances the rotor. You can however press the reset button, re-entering the initial settings and start again.
Give it a go
You can only imagine what Alan Turing and Tommy Flowers would’ve made of an Arduino board. Even with its limited 16MHz/8-bit processing and 2KB RAM, an Arduino is capable of far more complex encryption than we’re performing here. Sure, you can use one of the free online or Android apps to do the same thing, but hopefully, this project will give you a glimpse into the world of cryptography and more importantly, a feel for just what the cryptanalysts at Bletchley Park were able to achieve with far less technology.
Today, the Enigma machine is still being used – this time to inspire the next generation into studying maths and science through Cambridge University’s The Enigma Project.
Enigma army messages were originally transmitted over radio in five-letter groups with operators splitting decoded groups to form the original words or ‘plain text’. Built your Arduino Enigma? Try decoding this message:
I-II-III APC MAG DY AP CM
OLCRH ICCKD CWWMO ZQFNR ISPCI KREKM VMSLI BJZGM DXDEJ WXQZQ EI
You’ll find the software or ‘sketch’ for this project on our Arduino page. Copy the contents of the libraries folder into your Arduino IDE’s libraries folder and create a new project using the .ino file, compile it and flash it to your Arduino Uno board. The software comes ‘as is’, with no warranty – it’s been thoroughly tested but you use it at your own risk.
Learn more about Enigma – and how it was cracked
It’s now common knowledge how Enigma was cracked by the cryptanalysts at the famed Bletchley Park establishment in Buckinghamshire, north of London, but ‘Ultra’ still had top-secret classification until the 1970s. The sixth part of the excellent 1970s BBC series The Secret War is devoted to Bletchley Park. The series itself is dated but still a terrific introduction into the scientific and technical advancements made during that period, featuring interviews with many of those involved at the time, including Colossus engineer Tommy Flowers. Search for ‘The Secret War Still Secret’ on YouTube.