annotate content/Coding/018-pyenigma.rst @ 5:4b5cdcc351c5

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