bgneal@4: Introducing Py-Enigma bgneal@4: ##################### bgneal@4: bgneal@4: :date: 2012-06-06 18:45 bgneal@4: :tags: Python, Py-Enigma, Enigma bgneal@4: :slug: introducing-py-enigma bgneal@4: :author: Brian Neal bgneal@7: :summary: I got the Enigma machine bug bad! I wrote a simulator in Python. bgneal@4: bgneal@4: Py-Enigma bgneal@4: --------- bgneal@4: bgneal@4: For some strange reason, I don't even remember why or how, I found myself bgneal@4: browsing the `Wikipedia page for the Enigma Machine bgneal@4: `_. I quickly became fascinated and bgneal@4: started surfing around some more. I found several Enigma machine simulators, bgneal@4: some of them even online. It suddenly occured to me that it would be a fun bgneal@4: project to try and write my own in Python_. I wanted to challenge myself to see bgneal@4: if I could figure it all without help, so I vowed not to look at anyone else's bgneal@4: source code. bgneal@4: bgneal@4: In order to write an Enigma simulator, you need a really good technical bgneal@4: explanation of how it worked, along with numerous details like how the rotors bgneal@4: were wired. Fortunately I very quickly found Dirk Rijmenants' incredible `Cipher bgneal@4: Machines and Cryptology website`_. In particular, his `Technical Details of the bgneal@4: Enigma Machine`_ page was exactly what I was looking for, a real gold mine of bgneal@4: information. bgneal@4: bgneal@4: I had a long Memorial Day weekend, so I spent many hours in front of the bgneal@4: computer, consuming every word of Dirk's explanations and trying to sort it all bgneal@4: out in my head. And so a very fun marathon hacking session began. In the end I bgneal@4: got it figured out, and you cannot believe how excited I was when I was able to bgneal@4: decode actual Enigma messages from World War II! bgneal@4: bgneal@4: And so `Py-Enigma `_ was born! Py-Enigma is bgneal@4: a historically accurate simulation library for war-time Enigma machines, written bgneal@4: in Python 3. I also included a simple command-line application for doing bgneal@4: scripting or quick commad-line experiments. bgneal@4: bgneal@4: Lessons learned bgneal@4: --------------- bgneal@4: bgneal@4: Since I didn't really know what I was doing at first, I wrote little classes for bgneal@4: each of the components and unit tested them. I'm really glad I did this because bgneal@4: not only did it find bugs, it also made me think harder about the problem and bgneal@4: made me realize I didn't understand everything. When you make a mistake in bgneal@4: cryptography, very often the answer you get is gibberish and there is no way to bgneal@4: tell how close you are to the right answer. This was almost the case here, and I bgneal@4: think I stumbled upon an Enigma weakness that the allied codebreakers must have bgneal@4: seen also. I had a bug in my rotor stepping algorithm, and I got the right bgneal@4: answer up until the point where the right rotor stepped the middle rotor, then bgneal@4: the output went all garbage. Once I noticed this, I was able to focus on the bgneal@4: stepping algorithm and find the bug. I'm sure the allied codebreakers must have bgneal@4: experienced the same thing when they were guessing what rotors were being used bgneal@4: for the message they were cracking. bgneal@4: bgneal@4: I also decided to use this little project to really learn Sphinx_. I had dabbled bgneal@4: around in it before when I contributed some documentation patches to Django_. I bgneal@4: think writing the documentation took almost as long as my research and coding, bgneal@4: but with Sphinx at least it was fun! It is a very useful and powerful package. I bgneal@4: also checked out the awesome `readthedocs.org `_ and bgneal@4: quickly got my documentation `hosted there bgneal@4: `_. What a fantastic service! Now whenever I bgneal@4: push changes to bitbucket my docs are automatically built on readthedocs! bgneal@4: bgneal@4: This was also my second project that I put up on PyPI. I'm a little bit more bgneal@4: comfortable with the packaging process, but it is still a bit bewildering given bgneal@4: all the choices. bgneal@4: bgneal@4: Conclusion bgneal@4: ---------- bgneal@4: bgneal@4: I hope folks who are interested in history, cryptography, World War II, and bgneal@4: Python find Py-Enigma useful and fun! You can get the code from either the bgneal@4: `Py-Enigma Bitbucket page`_ or from PyPI_. And a big thank-you to Dirk bgneal@4: Rijmenants! Without his hard work and detailed explanation, Py-Enigma would bgneal@4: have been considerably more difficult. bgneal@4: bgneal@4: .. _Python: http://www.python.org bgneal@4: .. _Cipher Machines and Cryptology website: http://users.telenet.be/d.rijmenants/index.htm bgneal@4: .. _Technical Details of the Enigma Machine: http://users.telenet.be/d.rijmenants/en/enigmatech.htm bgneal@4: .. _Sphinx: http://sphinx.pocoo.org/ bgneal@4: .. _Django: http://www.djangoproject.com/ bgneal@4: .. _Py-Enigma Bitbucket page: https://bitbucket.org/bgneal/enigma bgneal@4: .. _PyPI: http://pypi.python.org/pypi/py-enigma/