# HG changeset patch # User Brian Neal # Date 1338171634 18000 # Node ID ce384011521446f9e1a3fdf14ab72b4baa3b7396 # Parent 2ce2e8c5a5be1bba916d452c471b8727b327b711 Discovered a notch bug. Saving everything before trying to fix it. diff -r 2ce2e8c5a5be -r ce3840115214 enigma/machine.py --- 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.""" diff -r 2ce2e8c5a5be -r ce3840115214 enigma/plugboard.py --- 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'))) diff -r 2ce2e8c5a5be -r ce3840115214 enigma/tests/test_enigma.py --- 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])) diff -r 2ce2e8c5a5be -r ce3840115214 enigma/tests/test_plugboard.py --- 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)