I'm very pleased to announce yet another M-209 simulator written in Python, creatively called m209. Last summer I worked on Enigma simulators in both Python and C++, and I thought it would be fun to try another World War II-era crypto device. m209 is a Python 3 library and command-line utility for encrypting and decrypting text by simulating the operation of an actual M-209 device.

One fun part about doing something like this is researching the original device. It seems like there are more resources online about the M-209 than the Enigma. I even found an actual 1940's War Department training film on YouTube that explains how to operate the M-209, including the procedure for encrypting and decrypting messages! I want to thank Mark J. Blair for his very informative pages on the M-209 which were very helpful to me. Check out the m209 references section for these and other useful links.

The M-209 isn't as complex as the Enigma. That isn't meant to knock it. The M-209, while cryptographically not as secure as the Enigma, is a remarkable piece of mechanical engineering. It is much more portable and easier to operate compared to the Enigma. It has user-friendly features like printing to paper tape and a letter counter for backing up when mistakes are made. According to Wikipedia, about 140,000 of these machines were produced. They even come up on eBay a few times a year, and seem to go for between $1000 - $2000 USD. Maybe someday I can score an actual unit!

Coding the actual simulator wasn't all that hard. I spent much more time on the unit tests, documentation, and creating an application to generate key lists. Writing the documentation gave me some good practice with Sphinx, an awesome Python based documentation tool that uses the reStructured Text markup language.

Writing the key list generator was actually the hardest part. The procedure for creating key lists is spelled out in a M-209 manual from 1944 (which exists online as a series of photos). The procedure is kind of loosely specified, and a lot is left up to the soldier creating the key list. I came up with an ad-hoc, heuristic-based algorithm that works most of the time. If it got stuck it simply started over, and retried up to a certain number of attempts.

While researching the procedure, I noticed what appears to be a typo in the data tables in the manual that are used when developing a key list. On top of that I found several sets of initial numbers that I could not generate a key list from. In other words, using these starting numbers, my algorithm could not generate M-209 settings that satisfied the exit criteria for the procedure in the manual. After a while, I just removed those troublesome initial conditions as possible inputs. It would be interesting to return to this some day and write a program to search the solution space exhaustively to see if there really was a solution for these numbers. It could just be that my trial-and-error algorithm could not find a solution, even after tens of thousands of attempts. However this doesn't seem likely. I wonder if these initial settings caused lots of head scratching for the poor officer trying to create a key list.

In any event, if you are into this kind of thing, I hope you check out m209. Doing a project like this is a lot of fun. I enjoy doing the research, creating the code, and working on the test suite. I also get some practice with Python packaging and writing documentation with Sphinx.

Future enhancements include adding the ability to read Mark Blair's key lists that he created for his C++ simulator. This would make it easier for our two simulators to interoperate.

Links:


Comments

comments powered by Disqus