Note
This documentation is in draft form. Reports of any errors or suggestions for improvement are welcomed and should be submitted as new issues.
crypto-enigma¶
An Enigma machine simulator with rich textual display functionality for Python 2.7.
Currently support is only provided for those machine models in most widespread general use during the war years: the I, M3, and M4.
No attempt is made here to describe the operation of an Enigma machine. For information about how an Enigma machine works see the excellent and extensive resources available at the Crypto Museum.
Functionality and use¶
The package provides functionality for generating a machine configuration from a conventional specification, examining the state of a configuration, simulating the operation of a machine by stepping between states, and encoding messages:
Create a machine configuration (see the config_enigma_from_string
):
>>> from crypto_enigma import *
>>> cfg = EnigmaConfig.config_enigma_from_string(u'B-I-III-I EMO UX.MO.AY 13.04.11')
Encode messages (see the enigma_encoding
):
>>> cfg.enigma_encoding(u'TESTINGXTESTINGUD')
u'OZQKPFLPYZRPYTFVU'
>>> cfg.enigma_encoding(u'OZQKPFLPYZRPYTFVU')
u'TESTINGXTESTINGUD'
Show configuration details (see the config_string
):
>>> print(cfg.config_string(letter=u'X', format='internal', mark_func=lambda c: '(' + c + ')'))
X > ABCDEFGHIJKLMNOPQRSTUVW(X)YZ
P YBCDEFGHIJKLONMPQRSTXVW(U)AZ UX.MO.AY
1 HCZMRVJPKSUDTQOLWEXN(Y)FAGIB O 05 I
2 KOMQEPVZNXRBDLJHFSUWYACT(G)I M 10 III
3 AXIQJZ(K)RMSUNTOLYDHVBWEGPFC E 19 I
R YRUHQSLDPX(N)GOKMIEBFZCWVJAT B
3 ATZQVYWRCEGOI(L)NXDHJMKSUBPF I
2 VLWMEQYPZOA(N)CIBFDKRXSGTJUH III
1 WZBLRVXAYGIPD(T)OHNEJMKFQSUC I
P YBCDEFGHIJKLONMPQRS(T)XVWUAZ UX.MO.AY
T < CNAUJVQSLEMIKBZRGPHXDFY(T)WO
Simulate machine operation (see the print_operation
):
>>> cfg.print_operation(message=u'TESTING', show_step=True, mark_func=lambda c: '(' + c + ')')
0000 CNAUJVQSLEMIKBZRGPHXDFYTWO EMO 19 10 05
0001 T > UNXKGVERLYDIQBTWMHZ(O)AFPCJS EMP 19 10 06
0002 E > QTYJ(Z)XUPKDIMLSWHAVNBGROFCE EMQ 19 10 07
0003 S > DMXAPTRWKYINBLUESG(Q)FOZHCJV ENR 19 11 08
0004 T > IUSMHRPEAQTVDYWGJFC(K)BLOZNX ENS 19 11 09
0005 I > WMVXQRLS(P)YOGBTKIEFHNZCADJU ENT 19 11 10
0006 N > WKIQXNRSCVBOY(F)LUDGHZPJAEMT ENU 19 11 11
0007 G > RVPTWS(L)KYXHGNMQCOAFDZBEJIU ENV 19 11 12
Watch the machine as it runs for 500 steps:
>>> cfg.print_operation(steps=500, show_step=True, format='internal', overwrite=True)
Command line functionality¶
A command line script, enigma.py
, provides access to almost all the functionality of the API.
Encode messages:
$ enigma.py encode "B-I-III-I EMO UX.MO.AY 13.04.11" "TESTINGXTESTINGUD" OZQKPFLPYZRPYTFVU $ enigma.py encode "B-I-III-I EMO UX.MO.AY 13.04.11" "OZQKPFLPYZRPYTFVU" TESTINGXTESTINGUD
Show configuration details (explained in more detail in the command line help):
$ enigma.py show "B-I-III-I EMO UX.MO.AY 13.04.11" -l 'X' -H'()' -f internal X > ABCDEFGHIJKLMNOPQRSTUVW(X)YZ P YBCDEFGHIJKLONMPQRSTXVW(U)AZ UX.MO.AY 1 HCZMRVJPKSUDTQOLWEXN(Y)FAGIB O 05 I 2 KOMQEPVZNXRBDLJHFSUWYACT(G)I M 10 III 3 AXIQJZ(K)RMSUNTOLYDHVBWEGPFC E 19 I R YRUHQSLDPX(N)GOKMIEBFZCWVJAT B 3 ATZQVYWRCEGOI(L)NXDHJMKSUBPF I 2 VLWMEQYPZOA(N)CIBFDKRXSGTJUH III 1 WZBLRVXAYGIPD(T)OHNEJMKFQSUC I P YBCDEFGHIJKLONMPQRS(T)XVWUAZ UX.MO.AY T < CNAUJVQSLEMIKBZRGPHXDFY(T)WO
Simulate machine operation (explained in more detail command line help):
$ enigma.py run "B-I-III-I EMO UX.MO.AY 13.04.11" -m "TESTING" -t -H'()' 0000 CNAUJVQSLEMIKBZRGPHXDFYTWO EMO 19 10 05 0001 T > UNXKGVERLYDIQBTWMHZ(O)AFPCJS EMP 19 10 06 0002 E > QTYJ(Z)XUPKDIMLSWHAVNBGROFCE EMQ 19 10 07 0003 S > DMXAPTRWKYINBLUESG(Q)FOZHCJV ENR 19 11 08 0004 T > IUSMHRPEAQTVDYWGJFC(K)BLOZNX ENS 19 11 09 0005 I > WMVXQRLS(P)YOGBTKIEFHNZCADJU ENT 19 11 10 0006 N > WKIQXNRSCVBOY(F)LUDGHZPJAEMT ENU 19 11 11 0007 G > RVPTWS(L)KYXHGNMQCOAFDZBEJIU ENV 19 11 12
Watch the machine as it runs for 500 steps:
$ enigma.py run "c-β-VIII-VII-VI QMLI 'UX.MO.AY 01.13.04.11" -s 500 -t -f internal -o
Limitations¶
Note that the correct display of some characters used to represent components (thin Naval rotors) assumes support for Unicode, while some aspects of the display of machine state depend on support for combining Unicode. This is a known limitation that will be addressed in a future release.
Note also that at the start of any scripts that use this package, you should
from __future__ import unicode_literals
before any code that uses the API, or confiure IPython (in ipython_config.py
) with
c.InteractiveShellApp.exec_lines += ["from __future__ import unicode_literals"]
or explicitly supply Unicode strings (e.g., as in many of the examples here with u'TESTING'
).