pythonencodingenigma2

Problems with implementing the plugboard for an enigma build in python


It was a non-compulsory exercise to build an enigma 'like' machine for schoolwork at uni. There were several steps for guidance (first I had to implement one rotating wheel, then 3 of them, after this I had to implement the reflector, then the ability to set the starting position for the wheels). Until this point everything went fine (it passed every test), but when I tried to implement the plugboard (Steckerbrett) my build fails to do the test and after searching for a mistake for hours (that i could not find) I ask you to help me.

Here is my code (please note that I am still a noobie and that my code may not be the neatest):

    #Enigma build (everything included)
class Enigma:
    def __init__(self, wheels, reflector, starting_position, plugs, message):
        self.message = message
        self.wheels = Wheels(wheels, starting_position)
        self.reflector = reflector
        self.plugboard = Plugboard(plugs)

        self.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        self.wheels.set_starting_position()

    def execute(self):
        output = ""

        self.message = self.plugboard.forward(self.message)

        for char in self.message:
            if char in self.alphabet:
                coded_char = char

                if coded_char in self.plugboard.plugs_forward:
                    coded_char = self.plugboard.plugs_forward[coded_char]

                coded_char = coding_forward(coded_char, self.alphabet, self.wheels.wheel1)
                coded_char = coding_forward(coded_char, self.alphabet, self.wheels.wheel2)
                coded_char = coding_forward(coded_char, self.alphabet, self.wheels.wheel3)

                coded_char = coding_forward(coded_char, self.alphabet, self.reflector)

                coded_char = coding_backward(coded_char, self.alphabet, self.wheels.wheel3)
                coded_char = coding_backward(coded_char, self.alphabet, self.wheels.wheel2)
                coded_char = coding_backward(coded_char, self.alphabet, self.wheels.wheel1)

                output = "".join([output, coded_char])
                self.wheels.rotate()

        output = self.plugboard.backward(output)

        return output

class Wheels:
    def __init__(self, wheels, position):
        self.wheel1 = wheels[0]
        self.wheel2 = wheels[1]
        self.wheel3 = wheels[2]
        self.position = position

        self.rotation_counter = 0

    def rotate(self):
        self.rotation_counter += 1
        self.wheel1 = "".join([self.wheel1[1:], self.wheel1[0]]) 
        if self.rotation_counter % 26 == 0:
            self.wheel2 = "".join([self.wheel2[1:], self.wheel2[0]]) 
        if self.rotation_counter % 676 == 0:
            self.wheel3 = "".join([self.wheel3[1:], self.wheel3[0]]) 
            self.rotation_counter = 0

    def set_starting_position(self):
        turn1 = ord(self.position[0]) - ord('A')
        self.wheel1 = "".join([self.wheel1[turn1:],self.wheel1[:turn1]])

        turn2 = ord(self.position[1]) - ord('A')
        self.wheel2 = "".join([self.wheel2[turn2:],self.wheel2[:turn2]])

        turn3 = ord(self.position[2]) - ord('A')
        self.wheel3 = "".join([self.wheel3[turn3:],self.wheel3[:turn3]])

class Plugboard:
    def __init__(self, plugs):
        self.plugs_forward = {a:b for a,b in plugs}
        self.plugs_backward = {b:a for a,b in plugs}

    def forward(self, string):
        output = []
        for char in string:
            if char in self.plugs_forward:
                output.append(self.plugs_forward[char])
            else:
                output.append(char)
        return "".join(output)

    def backward(self, string):
        output = []
        for char in string:
            if char in self.plugs_backward:
                output.append(self.plugs_backward[char])
            else:
                output.append(char)
        return "".join(output)

def coding_forward(char, wheel_side_1, wheel_side_2):
    coded_char = ""
    char_ix = wheel_side_1.index(char)
    coded_char = wheel_side_2[char_ix]
    return coded_char

def coding_backward(char, wheel_side_1, wheel_side_2):
    coded_char = ""
    char_ix = wheel_side_2.index(char)
    coded_char = wheel_side_1[char_ix]
    return coded_char


user_input = input()
newEnigma = Enigma(["QOFJSPIRUTNLMXWBZEGYKCADVH","ZHQYASLFCTRPKUWDVBIMJXGOEN", "JQBVTIEUXANMYDKPSWLZHGFROC"],
"YRUHQSLDPXNGOKMIEBFZCWVJAT", "KEY", ["QA", "HJ", "KI", "EV", "GC", "PO", "LK", "NX", "AS", "TE"], user_input)
print(newEnigma.execute())

Now this "EGJTUABUVJWZCYURYOIDFZZKHZCWQQGMKTTTNADFMBNFOQXTSDGANPROIWLLBZJVRXIRQAINSHCURXQLPK" as one long string should give an English sentence (of course with only capitals and without spaces)

Thanks for the answers in advance!


Solution

  • You have several plugs plugged into E & A in your plugboard settings input; I think that might be creating your issues.:

    ["QA", "HJ", "KI", "EV", "GC", "PO", "LK", "NX", "AS", "TE"]
      ^A                ^E                            ^A    ^E  
    

    You might want to verify that the plugboard inputs conforms to requirements.

    here is an approach to do this:

    def verify_plugs(plug_connections):
    
        seen = set([letter for pairs in plug_connections for letter in pairs])
        return len(seen) == len(plug_connections) * 2
    
    
    plug_connections = ["QA", "HJ", "KI", "EV", "GC", "PO", "LK", "NX", "AS", "TE"]
    verify_plugs(plug_connections)
    

    output:

    False