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