File Exchange Pick of the Week

Our best user submissions

Enigma 3

Posted by Sean de Wolski,

Sean's pick this week is Enigma by Matt Brauer and the Engima MATLAB Team.



First, check out this video from the MathWorks' quarterly meeting where the Enigma app was debuted to the company!


On a flight home in March, The Imitation Game was showing on the plane. It was a great movie and made quick work of the flight, but I did not think anything of it being related to MATLAB or MathWorks. A few weeks later, Seth contacted a few of us to build a MATLAB version of the Enigma machine. Here's a summary of the email body:

Have you seen the movie “The Imitation Game” about Alan Turing’s work cracking the Enigma code in WWII? Turing had a student – Wilkinson. Wilkinson had a student named Moler... Cleve will talk at the company meeting about the connection between Alan Turing and MATLAB. Jack suggested we get a real Enigma machine, build a MATLAB App to emulate it, and send a message between the two.


  • We do not need to build it from scratch.
  • Anything we demo should fit with our coding standards.
  • We should design review it with Steve Eddins, Michelle Hirsch, Jack and Cleve.

The company meeting is 3 weeks from today. GO!

We quickly got to talking about high-level requirements and interface designs. These were the three biggest requirements:

  1. It needed to be easily usable at the command line without a user interface.
  2. The user interface needed to look good and use only images MathWorks owns.
  3. It needed to be able to handle multiple models of rotors and reflectors.

For the high-level functional design, it was decided that everything would be object oriented. By doing this, it would allow for the different mechanical components to act on their own and maintain state just like in the real machine. By using events and listeners, each component could announce events as they happen. If the user interface exists, it would hear those events and update automatically, separating the front end from the computation engine. Another nice side effect of using this design paradigm is that we could easily partition the work amongst the team.

The entire project was under Git source control with the remote repository living on an internal server. This allowed us to push and pull updates often but to also work offline.

Over the next few days we emailed new ideas back and forth to come up with a look and feel. After that, we split the work into pieces: pushing and pulling the changes from Git, each working on our own component, and emailing ideas/concerns back and forth. About a week later, we had a basic working prototype.

WWII Museum

In order to test the MATLAB version of our Enigma Machine, we were given a few hours to experiment with three real machines. These Enigma Machines were available at the World War II museum in Natick, Massachusetts, a few miles from MathWorks' headquarters. We had no idea what to expect at the museum, figuring the museum director, Kenneth Rendell, would probably use the machines in front of us. No. We were granted special full access to them for two hours; an amazingly generous gift from Kenneth. We took the machines apart, learned what rotors and reflectors were in these machines, etc. If you ever wondered about an engineering equivalent to a kid in a candy shop...

Two of the real machines could exchange messages with each other. However, our MATLAB version couldn't correctly decrypt messages from the real machines. We realized that we had not implemented the ring setting, an additional offset. It took about an hour of desperate circshift-ing by Corey and troubleshooting every setting on the machine to finally get the machines to talk.

During the troubleshooting time we used the real machines and did a few experiments recording the results for further testing back at the office. One experiment was hitting the same letter over and over again about 200 times recording the results of how everything shifted. The other experiment was to hit each letter from the initial rotor setting, reset the rotor, and hit the button again. All of this data became crucial later to identify a physical double stepping behavior in the actual Enigma Machines.

If you're ever in Natick, I'd encourage you to go to the World War II museum. The hours and tour schedule are on the website. It's far more interesting than the mall.

Using MATLAB Enigma

Let's take a quick walk through using MATLAB Engima. There are two ways you can use it, both with and without a user interface. Let's first use the UI.


Use the gear icon in the upper right-hand corner to configure the components and plugboard.

Use the mouse scroll wheel over the rotor or left-click and drag for the initial rotor settings.

Type your message using your keyboard or by pushing the letter keys on the screen! The keys and indicators will light up as you type.

Now you can morse code that message to your friend.

How about with the command line?

MyEnigma = enigma;

Select the rotors.

MyEnigma.Rotors = {'V' 'II' 'I'};

Set the ring offset.

MyEnigma.RingSettings = [1 11 5];

Select the reflector.

MyEnigma.Reflector = 'B';

Rotate the rotors to the desired position.

MyEnigma.RotorSettings = 'PBR';

Connect the plugboard.


Perform the encryption.

encryptedmsg = run(MyEnigma,'THANKYOUFORREADING');

The machine knows its state, so you can see that in the command line display.

 enigma M3 Emulator

                      L    C    R
   Rotors:            V   II    I
   Ring Offsets:      1   11    5
   Rotor Settings:    P    B    J
   Reflector:     B
   Wired Plug Connections:
     A C D E F G H I J K L M O P Q R S U V W Y Z 
     | | | | | | | | | | | | | | | | | | | | | | 
     G M K I L A Y E S D F C R V Z O J W P U H Q 
   Input Log:
   Output Log:

And finally, clear the log to cover your tracks in case your office is compromised.



This was obviously a really fun project for all involved. In terms of software, we demonstrated what can be done with MATLAB Graphics, fully utilized the integrated Git source control from within MATLAB, worked collaboratively on a short deadline, and demonstrated a good example of an architecture for building applications with a user interface.

Of course, we also learned a lot about how these machines work and what an engineering marvel they really are! Thanks again to Kenneth from the WWII museum for granting us access to the machines and helping with our questions.

For more information on the history and the algorithm implementation in MATLAB, check out this entry in Cleve's Corner.


Watch the video, give the app a try and let us know what you think here or leave a comment for the Enigma Team.

Get the MATLAB code

Published with MATLAB® R2015b

16 views (last 30 days)  | |


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.