Exemple #1
0
    def __init__(self, rotors: list):
        # Make an empty list of rotor objects
        self.rotors = []

        # Populate self.rotors with 3 rotor objects
        for order, rotor in enumerate(rotors):
            if rotor[0] == "I":
                # The Represent Function is enables setting the rotors with letters
                window_position = represent(rotor[1])
                ring_setting = represent(rotor[2])
                self.rotors.append(RotorI(window_position, ring_setting,
                                          order))
            elif rotor[0] == "II":
                window_position = represent(rotor[1])
                ring_setting = represent(rotor[2])
                self.rotors.append(
                    RotorII(window_position, ring_setting, order))
            elif rotor[0] == "III":
                window_position = represent(rotor[1])
                ring_setting = represent(rotor[2])
                self.rotors.append(
                    RotorIII(window_position, ring_setting, order))
            elif rotor[0] == "IV":
                window_position = represent(rotor[1])
                ring_setting = represent(rotor[2])
                self.rotors.append(
                    RotorIV(window_position, ring_setting, order))
            elif rotor[0] == "V":
                window_position = represent(rotor[1])
                ring_setting = represent(rotor[2])
                self.rotors.append(RotorV(window_position, ring_setting,
                                          order))
Exemple #2
0
    def __init__(self, pairs: str):
        # Initiate plugboard connections
        self.letter_pairs = {}

        # Populate plugboard settings
        pairs = pairs.split()

        for letter_pair in pairs:
            first_letter = represent(letter_pair[0])
            second_letter = represent(letter_pair[1])
            self.letter_pairs[first_letter] = second_letter
            self.letter_pairs[second_letter] = first_letter
Exemple #3
0
    def encrypt_decrypt(self, letter: int):
        # Encrypt or decrypts a letter

        # Feed the letter to the plugboard:
        if letter in self.plugboard.letter_pairs:
            plugboard_output_1 = self.plugboard.plug(letter)
        else:
            plugboard_output_1 = letter  # If there is no connection in plugboard, return the letter

        # Feed plugboard output to Rotor Set(First pass from right to left):
        rotor_output_1 = self.rotor_set.right_to_left(plugboard_output_1)

        # Feed rotor output to reflector
        reflector_output = self.reflector.reflect(rotor_output_1)

        # Feed reflector output back to rotor:
        rotor_output_2 = self.rotor_set.left_to_right(reflector_output)

        # Feed rotor output to plugboard again
        if rotor_output_2 in self.plugboard.letter_pairs:
            plugboard_output_2 = self.plugboard.plug(rotor_output_2)
        else:
            plugboard_output_2 = rotor_output_2  # If there is no connection in plugboard, return the letter

        # return the result as letter
        return represent(
            plugboard_output_2
        )  # Represent is added to Output the letter as an alphabetic letter
Exemple #4
0
def encode_decode(enigma_machine, message):
    # Encode or Decode the message
    message = message.replace(" ", "")  # Remove spaces in message
    converted_text = [
        enigma_machine.encrypt_decrypt(represent(i)) for i in message
    ]

    # Return the Message
    return "".join(converted_text)
    def left_to_right(self, letter: int):
        # The following code encodes a letter from right to left (First run across Rotors)
        # Within a rotor, INFORMATION flow is Flat Contact -> Window (INFORMATION) -> Wiring ->
        # Spring Contact ->PlugBoard/Right Rotor

        # Flat Contact -> Window (INFORMATION)
        window = (letter + self.window_position) % 26

        #  Window (INFORMATION) -> Wiring
        wiring = (window - self.ring_setting) % 26

        # Wiring -> Spring Contact
        spring_contact = (self.wiring.index(represent(wiring))) % 26

        # Spring Contact -> Plugboard
        plugboard = (spring_contact - self.window_position +
                     self.ring_setting) % 26

        # Return the letter
        return plugboard
    def right_to_left(self, letter: int):
        # The following code encodes a letter from right to left (First run across Rotors)
        # Within a rotor, INFORMATION flow is PlugBoard/Right Rotor  -> Spring Contact -> Wiring ->
        # Window (INFORMATION) -> Flat Contact

        # Plugboard -> Spring Contact
        spring_contact = (letter + self.window_position -
                          self.ring_setting) % 26

        # Spring Contact -> Wiring
        wiring = (represent(self.wiring[spring_contact])) % 26

        # Wiring -> Window (INFORMATION)
        window = (wiring + self.ring_setting) % 26

        # Window (INFORMATION) -> Flat Contact
        flat_contact = (window - self.window_position) % 26

        # Return the letter
        return flat_contact
 def reflect(self, letter: int):
     return represent(self.wiring[letter])