I have revisited all of my previous tinkering and I have tried a new codebase, which seems to be progress.
The output I am getting is: 4444626524
def enigma(starting_rotors, starting_positions, message): # Define the rotors and reflector rotors = { 1: [1, 3, 6, 0, 5, 4, 8, 7, 9, 2], 2: [0, 3, 5, 2, 6, 9, 1, 4, 8, 7], 3: [5, 9, 1, 7, 3, 8, 0, 2, 4, 6], 4: [1, 6, 5, 2, 9, 0, 7, 4, 3, 8] } reflector = [3, 6, 8, 0, 5, 4, 1, 9, 2, 7] selected_rotors = [rotors[i] for i in starting_rotors] rotor_positions = starting_positions[:] # Copy to avoid modifying the original encrypted_message = "" def rotate_rotors(): # Increment the rightmost rotor's position rotor_positions[-1] += 1 # Check for rotation carry-over for i in reversed(range(1, len(rotor_positions))): if rotor_positions[i] > 9: rotor_positions[i] = 0 rotor_positions[i - 1] += 1 for digit in message: digit = int(digit) # Forward pass for i, rotor in enumerate(selected_rotors): adjusted_input = (digit + rotor_positions[i]) % 10 digit = rotor[adjusted_input] # Reflection digit = reflector[digit] # # Reverse pass # for i in reversed(range(len(selected_rotors))): # rotor = selected_rotors[i] # position = rotor_positions[i] # # Find the digit mapping # digit = (rotor.index((digit - position) % 10) + position) % 10 # Reverse pass for i in reversed(range(len(selected_rotors))): rotor = selected_rotors[i] digit = rotor.index(digit) # Find the index directly, reflecting the true reverse pass logic encrypted_message += str(digit) rotate_rotors() return encrypted_message # Example usage starting_rotors = [1, 2, 3] # Rotors to use starting_positions = [0, 0, 0] # Initial positions of the rotors message = "1234567890" # The message to encrypt # Encrypt the message encrypted_message = enigma(starting_rotors, starting_positions, message) print(f"Encrypted message: {encrypted_message}")I haven't got something quite right, though. The first two digits are correctly encrypted, but after that all other digits are encrypted incorrectly. The expected output is: 4805344463
The output I am getting is: 4444626524