Making an Enigma simulator

When I was ten or so I read the book “Encyclopedia of Espionage” by Peter Wright. I was used to James Bond movies and it was of course exciting to see things explode, but I was even more excited by this book. Instead of a fictional action hero saving the world it gives examples of how a bit of smart thinking helped saving actual lives. Cool! One of the fields Wright was involved in was ELINT (Electronic Intelligence) and his book describes several efforts to break different codes and coding machines. Among them Ultra, the (successfull) British effort to break the Enigma machine (although credit for breaking the machine first should go to the Polish).

The Enigma machine was developed in Germany shortly after the First World War. There have been different versions but the central element was a system of rotors. Each rotor would scramble a letter to a different letter and this was repeated several times. After every keystroke (some of) the rotors would rotate and the encryption would change. If you type for example “AAA” it would encode to “FTZ”. This was a massive improvement over older methods where you could do for example frequency analysis to find the key (ie. count the occurance of letters to determine the key). This video gives a nice introduction to the older methods and some of the inner workings of the Enigma. I saw this video on a rainy Saturday and I was, again, fascinated. The system seemed to be straightforward enough – could I simulate how it worked?

My first choice for a simulation would be to use Python. It is an all-purpose language and I know it quite well. I use it for basically everything, from when I’m too lazy to find my pocket calculator to calculating the spectra we measure in the lab. However, I always wanted to learn Objective-C and Cocoa, the languages used to write Mac OS X and iOS apps.

Objective-C is a C-like language you can use to write independent command line apps or you can use it together with Cocoa. Cocoa is an “Application Programming Interface” with all the standard interface elements you find in Mac programs, for example the windows, buttons and textfields. This is nice because it saves the programmer a lot of work. It is also nice for the user because the same elements work the same in different programs. That most people now think “I never thought about that before” is a testament to the good job Apple did. (and if you wonder why the buttons in Matlab and some other programs behave a bit weird, it is because they don’t use Cocoa).

I don’t want to bore you with too much programming details, but it is challenging to learn a language by writing an encoder, since the letters coming out are (by definition) scrambled in seven to nine steps. Good luck finding out where you made the mistake! I also found out (again) how difficult it is to make a good user interface. Take for example one of the textfields for the rotor position. The user should type in a letter, but what if they use a number? For numbers in the range 1-26 the number will be converted to a letter, for other numbers and non-letter characters it will revert back to the old character. How do you implement something like that efficiently? And how do the textfield and the up/down buttons next to it work together? In Cocoa Apple supplies a lot of nice interface elements, but there is still a lot to do. And of course, Apple doesn’t supply a WWII-style plugboard so I had to make one myself. It is definitely the part that needs most improvement.

It is nice to play around with programming this way. Is anybody waiting for yet another enigma simulator? Nope. Is it cool to learn something new? Yes. Am I proud of what I made since that rainy Saturday? Certainly!

If you want to know more about the Enigma, you can find it here. That page also includes some links with much more information. The app (for Mac OS X 10.5 and above*) and some instructions can be found here.

*UPDATE: I changed it from 10.7 only to 10.5 and above.