Game – Entertainment Projects

Kid’s Game to Arduino Enigma Machine

The technology behind Enigma machines and the work done to crack them has influenced cryptography, cryptanalysis, and computer science in general.  If you’re unfamiliar with Enigma machines, it’s a device that was developed near the end of World War I, then used extensively by the Nazi regime in World War II, to encrypt messages.  The allies (the British in particular) worked tirelessly to break the code.  One of the father’s of modern computer science Alan Turing (of Turing Machine fame) worked on the British team that worked break the code.  Some estimate that because the allies were able to crack the code the war ended two years earlier than it would have otherwise.

The technology behind Enigma machines and the work done to crack them has influenced cryptography, cryptanalysis, and computer science in general.  If you're unfamiliar with Enigma machines, it's a device that was developed near the end of World War I, then used extensively by the Nazi regime in World War II, to encrypt messages.  The allies (the British in particular) worked tirelessly to break the code.  One of the father's of modern computer science Alan Turing (of Turing Machine fame) worked on the British team that worked break the code.  Some estimate that because the allies were able to crack the code the war ended two years earlier than it would have otherwise.

One thing to note, the Enigma’s encryption algorithm is pretty good, but not perfect.  One weakness is that a letter can never be encrypted as itself which eliminates at least on character and can make it easier to crack.  Most of the issues the Nazi’s had that made it crackable had to do with their processes as they used the machine.  Key exchange is hard in a system like this where you don’t really have an out of band method to exchange keys.

If you want a detailed description of how Enigma machines work check out the Wikipedia article( http://en.wikipedia.org/wiki/Enigma_machine  ), or the many other articles on the internet, I’ll just give a quick overview here.

The Enigma machine basically consisted of three or more rotors that had contacts on both sides.  The rotors are set to the encryption or decryption “key” then a button is pushed which goes through the wiring in the rotors, then lights up a lamp corresponding to the encrypted output (there’s a lamp for every letter in the alphabet).  To decrypt you simply start with the same key that was used to encrypt the data and type in the cipher text.  One thing significant about the Enigma machine is that the rotors rotate as each key is pressed, so the encryption key essentially changes with each character.

I was in a thrift store recently and saw a game with a screen and a keyboard and I thought, that would make a good Enigma machine (I’ve always wanted one, but I know my wife would kill me if I bought a real one $$$).  In this instructable I’ll show how I gutted an kids game and used an Arduino to hook up to the keypad and speaker.  The code’s not too bad (so far).  This version is a simple three rotor Enigma machine.  You can switch the rotors around and change the letters for the rotors.  Future plans are to all for more rotors, allow changing out the reflector, allow for spinning the ring around a rotor, and a plug board implementation.  This Enigma machine will be able to encode and decode messages to and from the real, old Enigma machines used during Wold War II.

Step 1: Parts and Tools

Parts and Tools Kid’s Game to Arduino Enigma Machine

Parts
1. A kid’s game with a keyboard and a screen (I chose the Fisher Price game in the picture, got it at a local thrift store)
2. An Arduino (I used an Arduino Pro from Sparkfun).
3. Maybe a screen (you may be able to patch into the screen that comes with the game).

Tools
1. Mulitmeter
2. Soldering Iron
3. Maybe a Dremel type tool if you’re replacing the screen or doing other mods to the case

Step 2: Open It Up

Open It Up Kid’s Game to Arduino Enigma Machine

First, grab the game, remove the screws and open it up.  After I did that I had to remove a few more screws to get to the keyboard.  I cut the wires that connected the keyboard to the other PCB inside the game.

Step 3: Figure Out the Keyboard

Figure Out the Keyboard Kid’s Game to Arduino Enigma Machine

I pulled the rubber buttons off the keypad PCB so I could figure out how it was all wired up. To figure out how it’s wired up a used a multimeter on the Ohm meter setting to test for continuity. I put one lead on the row of pins on the cable the came off the keyboard to the “brains” of the game, and the other on the bare metal on the button circuit board for each button. I just used a sheet of paper to keep track of what button went to what pins.

Step 4:

At this point it was apparent what pins on the cables coming out of the board were the columns and rows of the keyboard matrix. I used this keypad library http://www.arduino.cc/playground/Main/KeypadTutorial on my Arduino to control the keypad. In my case it was a 5×8 keypad, so I need 13 pins. I ended up using Digital pins 2 and 4-13 and Analog pins 0 and 1. I didn’t use Digital pin 3 because I wanted that for driving the speaker in the game.

I kind of cheated when I mapped the key values. I basically just put bogus unique characters in the 2 dimensional array that defines the keys then I just started pushing buttons and had my Arduino output the keys pushed to serial. Then I could look at the output to set the 2 dimensional array for the library to the right values.

That seemed kind of confusing so I’ll do an example. As part of the keypad library you define a 2 dimensional array so it knows what keys correspond to the values on the matrix. So basically it’s something like this:

char keys[ROWS][COLS] = {
{‘<‘,’>’,’+’,’ ‘,’5′,’4′,’3′,’2’},
{‘^’,’*’,’%’,’#’,’6′,’1′,’Z’,’Y’},
{‘X’,’W’,’V’,’U’,’T’,’S’,’R’,’Q’},
{‘P’,’O’,’N’,’M’,’L’,’K’,’J’,’I’},
{‘H’,’G’,’F’,’E’,’D’,’C’,’B’,’A’}
};

I’d then push a button on the keypad (‘A’ for example). Then I’d look at what came out on the serial console (say it was ‘W’, then I’d go and replace the ‘W’ in the matrix with an ‘A’). After I went through all the keys then I had the keypad all mapped up.

Step 5: Modding the Case

My next step was modifying the case of the game.  I had to do a couple of things.  First I wanted to put a different screen in that was a little bigger, so I had to cut part of the case off.  There was also a handle in the back that made it hard to fit everything in.  I just traced out what I needed to cut and used a Dremel tool with a cutting disk to cut the case.

The pictures show the window before the mod, then with my new screen in it.  I used an I2C/Serial LCD from web4robot.

Step 6: Hooking Up the Keyboard and Screen to the Arduino

Now I needed to wire stuff up.  I used a female header on the Arduino and soldered wires right onto the pins.  You could also create a shield and solder onto that as well (in fact that’s probably a better way to do it, but this works for me).  Solder HO!

Step 7: The Code:Overview

The meat of the code is pretty simple.  I have an array for each rotor type, then I just needed to keep track of what rotor was in what slot, and what position the rotor was currently in.  Probably the easiest way to figure out what the code needs to do is to go through the example of this paper enigma machine.  http://mckoss.com/Crypto/Paper%20Enigma.pdf  .  Basically we need to take the input index, look at what letter is in that spot, then figure out what slot that should map to (it’s basically the where the letter ends up in a char array of the alphabet and the offset of the rotor).

The code also needs to handle the rotors moving.  The right most rotor moves one spot every time a key is pushed.  The middle rotor moves when the right most rotor hits a certain value (so it will turn 1/26 of the times the right one turns).  The left rotor turns 1/26 of the times the center rotor turns.  To do this I have an int that keeps track of the current position of the rotor and I add one to it when they need to turn.  When they get to 26 I set them back to 0 (it went all the way around the rotor so we’re starting again).

To swap out rotors I simply strncpy the rotor constants to the rotor in for the position.

You’ll notice I used a lot of example code in my code to do things like play tones, get keypad values, and address the I2C LCD.  I’m a big fan of libraries and example code.

Step 8: The Code: Rotors

You may ask “Why not just define them as a string, this seems like longhand”.  In other words why not do it like this:

char rotI[26] = “‘EKMFLGDQVZNTOWYHXUSPAIBRCJ”

I’ll admit this makes for much longer code, but to me it makes more sense visually to see it down (I can’t think of a time I’ve ever done this before, but with this one I think it works).  Basically since these are rotors (and I was copying the values off a paper Enigma machine) then that’s the way I chose to do it.

In addition to defining the reflectors I defined what number each rotor should turn the next rotor on.  In other words, when RI is in position 17, then the rotor to the left should turn.

const int spinRI = 17;
const int spinRII = 5;
const int spinRIII = 22;

Because the rotors can be put in any of the three slots you see in the screen I copy the constant spin values into ints that change anytime a different rotor is put in a slot (r1 is the left most rotor, r2 is the center rotor, r3 is the right most rotor).

int spin_r1 = spinRI;
int spin_r2 = spinRII;
int spin_r3 = spinRIII;

The following variables keep track of what rotor is in what slot.  This is used so you know when you change rotors in a slot what rotor next one will be

int cur_r1 = 1;
int cur_r2 = 2;
int cur_r3 = 3;

I also define the reflector.  Basically what this is saying is if an input comes in at position 3, then it should go back out position 7 for example (since the value in position 3 is 7).  The reflector basically sends the signal back through the rotors in reverse order

For More Details: Kid’s Game to Arduino Enigma Machine

Tags

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button
Close
Close