Arduino Projects: Enigma Cipher Machine

enigma-2

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.

The design

ardenigMore 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

displayElectrically, 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.

Software emulation

enigma-1There’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.

plugboardThe 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).

keyboardAt 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.

Decode this!

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

circuitWhere to find the software

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.

  • Gavin Stephanson

    Fantastic, thank you for this!

    • James Rybak

      Gavin,

      I don’t understand Arduino code very well. Could you please give me the pinout numbers for connecting the LCD to the Arduino? Please send it by email to jprybak@bresnan.net Thanks.

      Jim Rybak

      • Are you using a 1602 shield? The majority of them should just plug in and work.
        Cheers.
        Darren.

        • James Rybak

          Thanks for your suggestion. I will use a 1602 shield.

          Best regards,

          Jim

      • Gavin Stephanson

        It’s been so long sincei built it, and my schematic is lost among paperwork. I built it based off a bare atmega328p, but by the layout of the d pins it seems to be plug and play for the 1602.

        • James Rybak

          Thanks, again. I have ordered a 1602 shield for my Arduino. All should be OK.
          Jim

      • Gavin Stephanson

        Sorry it has been so long James, but i after cleaning up my paperwork (doing a little bit every now and then) i found my schematic. If you are still interested; Arduino pins will be a#, lcd pins will be l#, potentiometer pins will be p#, and keyboard pins will be k#. I set it up to use a usb keyboard, it can be just as easy to use a ps2 kb.
        L1-l5:k3:p3:l15:ground
        L2-k4:a5V:p1
        L3-p2
        L4-a8
        L6-a9
        L11-a4
        L12-a5
        L13-a6
        L14-a7
        L16-a3.3v/a5v( back light)
        A3-k5
        A11-k1
        My schematic is drawn with reference to a bare atmega328p as well if you h would kd like.

        • James Rybak

          Gavin,

          Thank you very much. The Arduino Enigma project is still on my “to do” list so this will be very helpful. If you could email the schematic, that would be helpful as well.
          I’ve been spending time lately learning how to use the Particle Photon for controlling lights and appliances using WiFi and the internet.
          Again, thanks.

          Best regards,

          Jim

  • James Rybak

    Gavin,

    Thank you very much. The Arduino Enigma project is still on my “to do”
    list so this will be very helpful. If you could email the schematic, that would
    be helpful as well.

    I’ve been spending time lately learning how to use the Particle Photon for
    controlling lights and appliances using WiFi and the internet. It’s really fun.

    Again, thanks.

    Best regards,

    Jim

    • Gavin Stephanson

      I could if i had your email. I could probably redraw it in a cad program so it is easier to read, my current is hand drawn.

  • James Rybak

    My email address is jprybak@bresnan.net

    Thank you very much.

    Jim

  • James Rybak

    Darren,

    I finally got my other projects out of the way and built a copy of your Arduino Enigma machine simulator. It works great! If I knew more about writing Arduino code, I’d try to add simulation of the Enigma M4 machine as well. Thanks for making this project available and for the help you provided.

    Jim Rybak
    Grand Junction, CO
    USA

  • fabio antonini

    Hi Darren
    great job! I have really appreciated it.
    I’d like to use your program to simulate an end-to-end encrypted communication between a couple of Arduino boards. Have you developed also a de-crypt function? I have two Arduino boards connected through the serial. From the transmitter the encrypted data are sent to the receiver, but I need the de-crypt function to show the original phrase. Have you any feedback or hint?
    thanks a lot
    fabio