Mercurial > public > enigma
changeset 17:ce3840115214
Discovered a notch bug. Saving everything before trying to fix it.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 27 May 2012 21:20:34 -0500 |
parents | 2ce2e8c5a5be |
children | f4051bcac8c5 |
files | enigma/machine.py enigma/plugboard.py enigma/tests/test_enigma.py enigma/tests/test_plugboard.py |
diffstat | 4 files changed, 64 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/enigma/machine.py Sun May 27 19:41:59 2012 -0500 +++ b/enigma/machine.py Sun May 27 21:20:34 2012 -0500 @@ -108,9 +108,8 @@ if len(val) != 3: raise EnigmaError("Bad display value") - start = 0 if self.rotor_count == 3 else 1 - for i, r in enumerate(range(start, self.rotor_count)): - self.rotors[r].set_display(val[i]) + for i, rotor in enumerate(reversed(self.rotors)): + rotor.set_display(val[2 - i]) def get_display(self): """Returns the operator display as a string."""
--- a/enigma/plugboard.py Sun May 27 19:41:59 2012 -0500 +++ b/enigma/plugboard.py Sun May 27 21:20:34 2012 -0500 @@ -114,7 +114,7 @@ m, n = p.split('/') m, n = int(m), int(n) except ValueError: - raise PlugboardError('invalid pair: %s' % p) + raise PlugboardError('invalid pair: %s' % str(p)) wiring_pairs.append((m - 1, n - 1)) else: @@ -123,12 +123,12 @@ for p in pairs: if len(p) != 2: - raise PlugboardError('invalid pair: %s' % p) + raise PlugboardError('invalid pair: %s' % str(p)) m = p[0] n = p[1] if m not in WEHRMACHT_LABELS or n not in WEHRMACHT_LABELS: - raise PlugboardError('invalid pair: %s' % p) + raise PlugboardError('invalid pair: %s' % str(p)) wiring_pairs.append((ord(m) - ord('A'), ord(n) - ord('A')))
--- a/enigma/tests/test_enigma.py Sun May 27 19:41:59 2012 -0500 +++ b/enigma/tests/test_enigma.py Sun May 27 21:20:34 2012 -0500 @@ -48,3 +48,44 @@ plain_text = self.machine.process_text(self.CIPHER_TEXT) self.assertEqual(plain_text, self.PLAIN_TEXT) + + +class ActualCipherTestCase(unittest.TestCase): + """This example taken from Dirk Rijmenants' simulator manual.""" + + def setUp(self): + + ring_settings = [ord(c) - ord('A') for c in 'BUL'] + + self.machine = EnigmaMachine.from_key_sheet( + rotors='II IV V', + reflector='B', + ring_settings=ring_settings, + plugboard_settings='AV BS CG DL FU HZ IN KM OW RX') + + def test_decrypt(self): + + # retrieve the message key + self.machine.set_display('WXC') + msg_key = self.machine.process_text('KCH') + + # set the message key + self.machine.set_display(msg_key) + + ciphertext = ( + 'RFUGZ EDPUD NRGYS ZRCXN' + 'UYTPO MRMBO FKTBZ REZKM' + 'LXLVE FGUEY SIOZV EQMIK' + 'UBPMM YLKLT TDEIS MDICA' + 'GYKUA CTCDO MOHWX MUUIA' + 'UBSTS LRNBZ SZWNR FXWFY' + 'SSXJZ VIJHI DISHP RKLKA' + 'YUPAD TXQSP INQMA TLPIF' + 'SVKDA SCTAC DPBOP VHJK') + + # remove spaces and the Kenngruppen + ciphertext = ciphertext.replace(' ', '')[5:] + plaintext = self.machine.process_text(ciphertext) + + print("'{}'\n\n'{}'\n\n'{}'\n\n".format(msg_key, ciphertext[:11], + plaintext[:11]))
--- a/enigma/tests/test_plugboard.py Sun May 27 19:41:59 2012 -0500 +++ b/enigma/tests/test_plugboard.py Sun May 27 21:20:34 2012 -0500 @@ -92,3 +92,21 @@ self.assertEqual(p.signal(n), n - 1) else: self.assertEqual(n, p.signal(n)) + + def test_wiring2(self): + + stecker='AV BS CG DL FU HZ IN KM OW RX' + + wiring = {} + pairs = stecker.split() + for p in pairs: + m, n = ord(p[0]) - ord('A'), ord(p[1]) - ord('A') + wiring[m] = n + wiring[n] = m + + p = Plugboard.from_key_sheet(stecker) + for n in range(26): + if n in wiring: + self.assertEqual(p.signal(n), wiring[n]) + else: + self.assertEqual(p.signal(n), n)