Python Forum
Enigma Machine, I need help!
Thread Rating:
  • 3 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Enigma Machine, I need help!
#45
I have the incrementor working properly, (at least in the forward direction, bucause I'm not sure how to use the notches
on the reverse trip.

What I'm still not sure of is the way the rotor's rotation affects the cipher output,
because following Tony Sands example, it doesn't seem to have an effect. In addition,
I'm sure I've got the rotation increment correct in the forward direction, but still not clear on the reverse direction action.

New code Incrementor.py with full test (built into script) is now on github:
Testing against the Tony Sands example, here are the results:

First the code:
import json
import EnigmaPaths


class Incrementor:
    def __init__(self, rinfo, start_index=0):
        """
        Initialize - load rotor info, set starting indexes, get cipher
        :param rinfo:
        """
        self.epath = EnigmaPaths.EnigmaPaths()

        with self.epath.enigma_info.open() as f:
            self.init_data = json.load(f)

        self.cipher = rinfo['cipher']
        self.notch = rinfo['notches'][0]
        self.alpha = self.init_data['unencoded']
        self.index = start_index
        self.max_index = len(self.cipher) - 1

    def next_index(self):
        self.index += 1
        if self.index > self.max_index:
            self.index = 0
        return self.index

    def get_next_item(self, alpha_char, direction='f', preincrement=False):
        index = self.index
        if preincrement:
            index = self.next_index()
        if direction == 'f':
            aidx = self.alpha.index(alpha_char)
            item = self.cipher[aidx]
        else:
            cidx = self.cipher.index(alpha_char)
            item = self.alpha[cidx]
        advance = (self.alpha[index] == self.notch)
        if not preincrement:
            index = self.next_index()
        return item, advance

    def set_initial_index(self, index):
        if index >= len(self.cipher):
            print(f'index out of range, valid range is 0 - {self.max_index}')
        else:
            self.index = index

    def query_cipher(self):
        return self.cipher

    def query_index(self):
        return self.index

    def query_notch(self):
        return self.notch

def testit():
    # Order if progression is from right to left (rotors)
    # Assume 'B' selected for reflector
    # Our test letter.
    test_letter = 'G'

    # Setup right rotor(start index = 2, set by set_initial_index):
    rinfo = {'name': 'rotor3', 'cipher': 'BDFHJLCPRTXVZNYEIWGAKMUSQO', 'notches': ['W']}
    r_incrementor = Incrementor(rinfo)

    # Get reflector here (needed init_data to be visible
    test_reflector = r_incrementor.init_data['reflector_B']

    r_incrementor.set_initial_index(2)
    print(f'\nright alpha:  {r_incrementor.alpha}')
    print(f'right cipher: {r_incrementor.query_cipher()}')
    print(f'right index:  {r_incrementor.query_index()}')
    print(f'right notch:  {r_incrementor.query_notch()}')

    # Setup middle rotor (start index = 4 'S'):
    rinfo = {'name': 'rotor2', 'cipher': 'AJDKSIRUXBLHWTMCQGZNPYFVOE', 'notches': ['F']}
    m_incrementor = Incrementor(rinfo, start_index=4)
    print(f'\nmiddle alpha:  {m_incrementor.alpha}')
    print(f'middle cipher: {m_incrementor.query_cipher()}')
    print(f'middle index:  {m_incrementor.query_index()}')
    print(f'middle notch:  {m_incrementor.query_notch()}')

    # Setup left rotor (start index = 16):
    rinfo = {'name': 'rotor1', 'cipher': 'EKMFLGDQVZNTOWYHXUSPAIBRCJ', 'notches': ['R']}
    l_incrementor = Incrementor(rinfo, start_index=16)
    print(f'\nleft alpha:   {l_incrementor.alpha}')
    print(f'left cipher:  {l_incrementor.query_cipher()}')
    print(f'left index:   {l_incrementor.query_index()}')
    print(f'left notch:   {l_incrementor.query_notch()}')

    # Test one letter through entire process

    # First get right rotor cipher and advance rotor
    ritem, advance = r_incrementor.get_next_item(test_letter, direction='f')
    print(f'\nright rotor cipher: in: {test_letter}, out: {ritem}, advance: {advance}')
    # if advance is True increment middle rotor by 1
    # ignore cipher, in each case but check advance again
    if advance:
        # This means slot was encountered on middle advance, so
        #  have to advance left!
        mitem, advance = m_incrementor.get_next_item(ritem, direction='f')
        if advance:
            # This means middle slot was encountered on advance
            litem, advance = l_incrementor.get_next_item(mitem, direction='f')

    # Get middle rotor cipher and advance rotor
    mitem, advance = m_incrementor.get_next_item(ritem, direction='f')
    print(f'\nmiddle rotor cipher: in: {ritem}, out: {mitem}, advance: {advance}')
    if advance:
        # This means middle slot was encountered on advance
        litem, advance = l_incrementor.get_next_item(mitem, direction='f')

    litem, advance = l_incrementor.get_next_item(mitem, direction='f')
    print(f'\nleft rotor cipher: in: {mitem} out: {litem}, advance: {advance}')
    # Read up on what to do if advance = true, is the middle
    # advanced??

    # run through reflector
    aidx = l_incrementor.alpha.index(litem)
    refl_item = test_reflector[aidx]
    print(f'\nreflector output: in: {litem}, out: {refl_item}')

    # reverse trip
    print('Return trip')
    litem, advance = l_incrementor.get_next_item(refl_item, direction='r')
    print(f'\nleft rotor cipher: in: {refl_item}, out: {litem}, advance: {advance}')
    # Read up on what to do if advance = true, is the middle
    # advanced??

    # Get middle rotor cipher and advance rotor
    mitem, advance = m_incrementor.get_next_item(litem, direction='r')
    print(f'\nmiddle rotor cipher: in: {litem}, out: {mitem}, advance: {advance}')

    ritem, advance = r_incrementor.get_next_item(mitem, direction='r')
    print(f'\nright rotor cipher: in: {mitem}, out: {ritem}, advance: {advance}')

    print(f'\n\nletter in: {test_letter}, letter out: {ritem}')

if __name__ == '__main__':
    testit()
and built in test results:
Output:
right alpha:  ABCDEFGHIJKLMNOPQRSTUVWXYZ right cipher: BDFHJLCPRTXVZNYEIWGAKMUSQO right index:  2 right notch:  W middle alpha:  ABCDEFGHIJKLMNOPQRSTUVWXYZ middle cipher: AJDKSIRUXBLHWTMCQGZNPYFVOE middle index:  4 middle notch:  F left alpha:   ABCDEFGHIJKLMNOPQRSTUVWXYZ left cipher:  EKMFLGDQVZNTOWYHXUSPAIBRCJ left index:   16 left notch:   R right rotor cipher: in: G, out: C, advance: False middle rotor cipher: in: C, out: D, advance: False left rotor cipher: in: D out: F, advance: False reflector output: in: F, out: S Return trip left rotor cipher: in: S, out: S, advance: True middle rotor cipher: in: S, out: E, advance: True right rotor cipher: in: E, out: P, advance: False letter in: G, letter out: P
Reply


Messages In This Thread
Enigma Machine, I need help! - by Kimkuq - Nov-12-2017, 07:15 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-12-2017, 10:37 PM
RE: Enigma Machine, I need help! - by AceScottie - Nov-13-2017, 05:00 AM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-13-2017, 06:29 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-13-2017, 06:36 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-13-2017, 06:42 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-13-2017, 06:45 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-13-2017, 08:22 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-13-2017, 09:27 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-13-2017, 10:09 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-14-2017, 05:38 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-14-2017, 05:47 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-14-2017, 06:25 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-15-2017, 12:54 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-15-2017, 02:26 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-16-2017, 06:08 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-16-2017, 02:02 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-16-2017, 09:56 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-17-2017, 04:23 AM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-17-2017, 10:08 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-18-2017, 01:48 AM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-18-2017, 10:09 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-19-2017, 01:40 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-19-2017, 10:06 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-20-2017, 05:45 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-20-2017, 08:28 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-21-2017, 01:53 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-21-2017, 02:21 AM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-21-2017, 11:50 AM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-21-2017, 10:36 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-22-2017, 09:18 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-21-2017, 11:52 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-22-2017, 11:00 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-22-2017, 02:20 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-22-2017, 04:24 PM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-22-2017, 05:12 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-22-2017, 08:32 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-22-2017, 09:03 PM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-22-2017, 08:48 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-22-2017, 08:51 PM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-22-2017, 09:01 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-22-2017, 09:06 PM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-22-2017, 09:28 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-22-2017, 10:41 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-24-2017, 09:18 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-24-2017, 11:48 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-25-2017, 05:09 AM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-26-2017, 11:31 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-26-2017, 01:56 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-26-2017, 09:53 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-27-2017, 03:03 AM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-28-2017, 08:29 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-29-2017, 01:42 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Dec-02-2017, 02:20 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-02-2017, 02:50 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-04-2017, 01:49 AM
RE: Enigma Machine, I need help! - by Kimkuq - Dec-06-2017, 09:58 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-06-2017, 11:04 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-07-2017, 02:08 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-08-2017, 11:35 AM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-16-2017, 02:08 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-18-2017, 05:53 AM
RE: Enigma Machine, I need help! - by Kimkuq - Dec-18-2017, 09:06 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-19-2017, 03:26 AM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-23-2017, 02:22 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Dec-23-2017, 02:19 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-27-2017, 08:26 PM
RE: Enigma Machine, I need help! - by sparkz_alot - Dec-27-2017, 09:27 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-28-2017, 01:49 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Numeric Enigma Machine idev 9 699 Mar-29-2024, 06:15 PM
Last Post: idev
  Enigma Decoding Problem krisarmstrong 4 833 Dec-14-2023, 10:42 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020