Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Numeric Enigma Machine
#4
I have revisited all of my previous tinkering and I have tried a new codebase, which seems to be progress.

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
Reply


Messages In This Thread
Numeric Enigma Machine - by idev - Mar-27-2024, 09:50 PM
RE: Numeric Enigma Machine - by bowlofred - Mar-27-2024, 10:52 PM
RE: Numeric Enigma Machine - by idev - Mar-28-2024, 01:54 AM
RE: Numeric Enigma Machine - by idev - Mar-28-2024, 03:38 AM
RE: Numeric Enigma Machine - by bowlofred - Mar-28-2024, 04:44 AM
RE: Numeric Enigma Machine - by idev - Mar-28-2024, 01:30 PM
RE: Numeric Enigma Machine - by bowlofred - Mar-28-2024, 06:07 AM
RE: Numeric Enigma Machine - by idev - Mar-28-2024, 12:45 PM
RE: Numeric Enigma Machine - by idev - Mar-28-2024, 02:17 PM
RE: Numeric Enigma Machine - by idev - Mar-29-2024, 06:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Enigma Decoding Problem krisarmstrong 4 878 Dec-14-2023, 10:42 AM
Last Post: Larz60+
Question Numeric Anagrams - Count Occurances monty024 2 1,576 Nov-13-2021, 05:05 PM
Last Post: monty024
  How to get datetime from numeric format field klllmmm 3 2,070 Nov-06-2021, 03:26 PM
Last Post: snippsat
  Extract continuous numeric characters from a string in Python Robotguy 2 2,726 Jan-16-2021, 12:44 AM
Last Post: snippsat
  How to calculate column mean and row skip non numeric and na Mekala 5 5,100 May-06-2020, 10:52 AM
Last Post: anbu23
  Alpha numeric element list search rhubarbpieguy 1 1,851 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  convert a character to numeric and back Skaperen 2 2,177 Jan-28-2020, 09:32 PM
Last Post: Skaperen
  are numeric types passed by value or reference? rudihammad 4 2,703 Nov-19-2019, 06:25 AM
Last Post: rudihammad
  'Age' categorical (years -months -days ) to numeric Smiling29 4 3,018 Oct-17-2019, 05:26 PM
Last Post: Smiling29
  how to do a numeric sort Skaperen 11 5,121 Jul-12-2019, 09:50 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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