annotate content/Coding/018-pyenigma.rst @ 8:e29fd75628d6

Turn on disqus comments.
author Brian Neal <bgneal@gmail.com>
date Sat, 01 Feb 2014 13:58:51 -0600
parents 49bebfa6f9d3
children 6e0d4799796d
rev   line source
bgneal@4 1 Introducing Py-Enigma
bgneal@4 2 #####################
bgneal@4 3
bgneal@4 4 :date: 2012-06-06 18:45
bgneal@4 5 :tags: Python, Py-Enigma, Enigma
bgneal@4 6 :slug: introducing-py-enigma
bgneal@4 7 :author: Brian Neal
bgneal@7 8 :summary: I got the Enigma machine bug bad! I wrote a simulator in Python.
bgneal@4 9
bgneal@4 10 Py-Enigma
bgneal@4 11 ---------
bgneal@4 12
bgneal@4 13 For some strange reason, I don't even remember why or how, I found myself
bgneal@4 14 browsing the `Wikipedia page for the Enigma Machine
bgneal@4 15 <http://en.wikipedia.org/wiki/Enigma_machine>`_. I quickly became fascinated and
bgneal@4 16 started surfing around some more. I found several Enigma machine simulators,
bgneal@4 17 some of them even online. It suddenly occured to me that it would be a fun
bgneal@4 18 project to try and write my own in Python_. I wanted to challenge myself to see
bgneal@4 19 if I could figure it all without help, so I vowed not to look at anyone else's
bgneal@4 20 source code.
bgneal@4 21
bgneal@4 22 In order to write an Enigma simulator, you need a really good technical
bgneal@4 23 explanation of how it worked, along with numerous details like how the rotors
bgneal@4 24 were wired. Fortunately I very quickly found Dirk Rijmenants' incredible `Cipher
bgneal@4 25 Machines and Cryptology website`_. In particular, his `Technical Details of the
bgneal@4 26 Enigma Machine`_ page was exactly what I was looking for, a real gold mine of
bgneal@4 27 information.
bgneal@4 28
bgneal@4 29 I had a long Memorial Day weekend, so I spent many hours in front of the
bgneal@4 30 computer, consuming every word of Dirk's explanations and trying to sort it all
bgneal@4 31 out in my head. And so a very fun marathon hacking session began. In the end I
bgneal@4 32 got it figured out, and you cannot believe how excited I was when I was able to
bgneal@4 33 decode actual Enigma messages from World War II!
bgneal@4 34
bgneal@4 35 And so `Py-Enigma <https://bitbucket.org/bgneal/enigma>`_ was born! Py-Enigma is
bgneal@4 36 a historically accurate simulation library for war-time Enigma machines, written
bgneal@4 37 in Python 3. I also included a simple command-line application for doing
bgneal@4 38 scripting or quick commad-line experiments.
bgneal@4 39
bgneal@4 40 Lessons learned
bgneal@4 41 ---------------
bgneal@4 42
bgneal@4 43 Since I didn't really know what I was doing at first, I wrote little classes for
bgneal@4 44 each of the components and unit tested them. I'm really glad I did this because
bgneal@4 45 not only did it find bugs, it also made me think harder about the problem and
bgneal@4 46 made me realize I didn't understand everything. When you make a mistake in
bgneal@4 47 cryptography, very often the answer you get is gibberish and there is no way to
bgneal@4 48 tell how close you are to the right answer. This was almost the case here, and I
bgneal@4 49 think I stumbled upon an Enigma weakness that the allied codebreakers must have
bgneal@4 50 seen also. I had a bug in my rotor stepping algorithm, and I got the right
bgneal@4 51 answer up until the point where the right rotor stepped the middle rotor, then
bgneal@4 52 the output went all garbage. Once I noticed this, I was able to focus on the
bgneal@4 53 stepping algorithm and find the bug. I'm sure the allied codebreakers must have
bgneal@4 54 experienced the same thing when they were guessing what rotors were being used
bgneal@4 55 for the message they were cracking.
bgneal@4 56
bgneal@4 57 I also decided to use this little project to really learn Sphinx_. I had dabbled
bgneal@4 58 around in it before when I contributed some documentation patches to Django_. I
bgneal@4 59 think writing the documentation took almost as long as my research and coding,
bgneal@4 60 but with Sphinx at least it was fun! It is a very useful and powerful package. I
bgneal@4 61 also checked out the awesome `readthedocs.org <http://readthedocs.org>`_ and
bgneal@4 62 quickly got my documentation `hosted there
bgneal@4 63 <http://py-enigma.readthedocs.org/>`_. What a fantastic service! Now whenever I
bgneal@4 64 push changes to bitbucket my docs are automatically built on readthedocs!
bgneal@4 65
bgneal@4 66 This was also my second project that I put up on PyPI. I'm a little bit more
bgneal@4 67 comfortable with the packaging process, but it is still a bit bewildering given
bgneal@4 68 all the choices.
bgneal@4 69
bgneal@4 70 Conclusion
bgneal@4 71 ----------
bgneal@4 72
bgneal@4 73 I hope folks who are interested in history, cryptography, World War II, and
bgneal@4 74 Python find Py-Enigma useful and fun! You can get the code from either the
bgneal@4 75 `Py-Enigma Bitbucket page`_ or from PyPI_. And a big thank-you to Dirk
bgneal@4 76 Rijmenants! Without his hard work and detailed explanation, Py-Enigma would
bgneal@4 77 have been considerably more difficult.
bgneal@4 78
bgneal@4 79 .. _Python: http://www.python.org
bgneal@4 80 .. _Cipher Machines and Cryptology website: http://users.telenet.be/d.rijmenants/index.htm
bgneal@4 81 .. _Technical Details of the Enigma Machine: http://users.telenet.be/d.rijmenants/en/enigmatech.htm
bgneal@4 82 .. _Sphinx: http://sphinx.pocoo.org/
bgneal@4 83 .. _Django: http://www.djangoproject.com/
bgneal@4 84 .. _Py-Enigma Bitbucket page: https://bitbucket.org/bgneal/enigma
bgneal@4 85 .. _PyPI: http://pypi.python.org/pypi/py-enigma/