8590e9b46aa9991cbebdd3d4deffeb97c51ecd75
1 from szyfrow
.support
.utilities
import *
2 from szyfrow
.support
.language_models
import *
5 class PocketEnigma(object):
6 """A pocket enigma machine
7 The wheel is internally represented as a 26-element list self.wheel_map,
8 where wheel_map[i] == j shows that the position i places on from the arrow
9 maps to the position j places on.
11 def __init__(self
, wheel
=1, position
='a'):
12 """initialise the pocket enigma, including which wheel to use and the
13 starting position of the wheel.
15 The wheel is either 1 or 2 (the predefined wheels) or a list of letter
18 The position is the letter pointed to by the arrow on the wheel.
21 [25, 4, 23, 10, 1, 7, 9, 5, 12, 6, 3, 17, 8, 14, 13, 21, 19, 11, 20, 16, 18, 15, 24, 2, 22, 0]
25 self
.wheel1
= [('a', 'z'), ('b', 'e'), ('c', 'x'), ('d', 'k'),
26 ('f', 'h'), ('g', 'j'), ('i', 'm'), ('l', 'r'), ('n', 'o'),
27 ('p', 'v'), ('q', 't'), ('s', 'u'), ('w', 'y')]
28 self
.wheel2
= [('a', 'c'), ('b', 'd'), ('e', 'w'), ('f', 'i'),
29 ('g', 'p'), ('h', 'm'), ('j', 'k'), ('l', 'n'), ('o', 'q'),
30 ('r', 'z'), ('s', 'u'), ('t', 'v'), ('x', 'y')]
32 self
.make_wheel_map(self
.wheel1
)
34 self
.make_wheel_map(self
.wheel2
)
36 self
.validate_wheel_spec(wheel
)
37 self
.make_wheel_map(wheel
)
38 if position
in string
.ascii_lowercase
:
39 self
.position
= pos(position
)
41 self
.position
= position
43 def make_wheel_map(self
, wheel_spec
):
44 """Expands a wheel specification from a list of letter-letter pairs
45 into a full wheel_map.
47 >>> pe.make_wheel_map(pe.wheel2)
48 [2, 3, 0, 1, 22, 8, 15, 12, 5, 10, 9, 13, 7, 11, 16, 6, 14, 25, 20, 21, 18, 19, 4, 24, 23, 17]
50 self
.validate_wheel_spec(wheel_spec
)
51 self
.wheel_map
= [0] * 26
53 self
.wheel_map
[pos(p
[0])] = pos(p
[1])
54 self
.wheel_map
[pos(p
[1])] = pos(p
[0])
57 def validate_wheel_spec(self
, wheel_spec
):
58 """Validates that a wheel specificaiton will turn into a valid wheel
61 >>> pe.validate_wheel_spec([])
62 Traceback (most recent call last):
64 ValueError: Wheel specification has 0 pairs, requires 13
65 >>> pe.validate_wheel_spec([('a', 'b', 'c')]*13)
66 Traceback (most recent call last):
68 ValueError: Not all mappings in wheel specificationhave two elements
69 >>> pe.validate_wheel_spec([('a', 'b')]*13)
70 Traceback (most recent call last):
72 ValueError: Wheel specification does not contain 26 letters
74 if len(wheel_spec
) != 13:
75 raise ValueError("Wheel specification has {} pairs, requires 13".
76 format(len(wheel_spec
)))
79 raise ValueError("Not all mappings in wheel specification"
81 if len(set([p
[0] for p
in wheel_spec
] +
82 [p
[1] for p
in wheel_spec
])) != 26:
83 raise ValueError("Wheel specification does not contain 26 letters")
85 def encipher_letter(self
, letter
):
86 """Enciphers a single letter, by advancing the wheel before looking up
87 the letter on the wheel.
89 >>> pe.set_position('f')
91 >>> pe.encipher_letter('k')
95 return self
.lookup(letter
)
96 decipher_letter
= encipher_letter
98 def lookup(self
, letter
):
99 """Look up what a letter enciphers to, without turning the wheel.
101 >>> pe.set_position('f')
103 >>> cat([pe.lookup(l) for l in string.ascii_lowercase])
104 'udhbfejcpgmokrliwntsayqzvx'
108 if letter
in string
.ascii_lowercase
:
110 (self
.wheel_map
[(pos(letter
) - self
.position
) % 26] +
116 """Advances the wheel one position.
118 >>> pe.set_position('f')
123 self
.position
= (self
.position
+ 1) % 26
126 def encipher(self
, message
, starting_position
=None):
127 """Enciphers a whole message.
129 >>> pe.set_position('f')
131 >>> pe.encipher('helloworld')
133 >>> pe.set_position('f')
135 >>> pe.encipher('kjsglcjoqc')
137 >>> pe.encipher('helloworld', starting_position = 'x')
140 if starting_position
:
141 self
.set_position(starting_position
)
144 transformed
+= self
.encipher_letter(l
)
148 def set_position(self
, position
):
149 """Sets the position of the wheel, by specifying the letter the arrow
152 >>> pe.set_position('a')
154 >>> pe.set_position('m')
156 >>> pe.set_position('z')
159 self
.position
= pos(position
)
163 def pocket_enigma_break_by_crib(message
, wheel_spec
, crib
, crib_position
):
164 """Break a pocket enigma using a crib (some plaintext that's expected to
165 be in a certain position). Returns a list of possible starting wheel
166 positions that could produce the crib.
168 >>> pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'h', 0)
170 >>> pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'he', 0)
172 >>> pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'll', 2)
174 >>> pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'l', 2)
176 >>> pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'l', 3)
178 >>> pocket_enigma_break_by_crib('aaaaa', 1, 'l', 3)
181 pe
= PocketEnigma(wheel
=wheel_spec
)
182 possible_positions
= []
183 for p
in string
.ascii_lowercase
:
185 plaintext
= pe
.decipher(message
)
186 if plaintext
[crib_position
:crib_position
+len(crib
)] == crib
:
187 possible_positions
+= [p
]
188 return possible_positions
190 if __name__
== "__main__":
192 doctest
.testmod(extraglobs
={'pe': PocketEnigma(1, 'a')})