Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Numeric Enigma Machine
#8
Okay, you made an interesting point (if it's a valid one) and I decided to try your test method:

# Define the rotors and reflector
rotors = {
    0: list(range(10)),
    1: list(range(10)),
    2: list(range(10)),
    3: list(range(10)),
    4: list(range(10))
}
reflector = [3, 6, 8, 0, 5, 4, 1, 9, 2, 7]


def apply_rotor(input_digit, rotor, position):
    # Adjust input based on the rotor's position
    adjusted_input = (input_digit - position) % 10
    # Apply the mapping
    output_digit = rotor[adjusted_input]
    return output_digit

def encrypt_digit(digit, rotor_positions, selected_rotors):
    # Convert digit from string to integer
    digit = int(digit)

    # Forward pass through the selected rotors
    for i in range(len(selected_rotors)):
        digit = apply_rotor(digit, selected_rotors[i], rotor_positions[i])
        print(f"Forward - Digit after rotor {i} : {digit}")

    # Reverse pass through the selected rotors
    for i in reversed(range(len(selected_rotors))):
        # For reverse path, adjust input based on the rotor's position before applying the mapping
        adjusted_input = (digit + rotor_positions[i]) % 10
        digit = selected_rotors[i].index(adjusted_input)
        print(f"Reverse - Digit after rotor {i} : {digit}")

    # Convert digit back to string for output
    return str(digit)

def rotate_rotors(rotor_positions):
    # Rotate the rightmost rotor after encrypting each digit
    rotor_positions[-1] += 1
    print("Rotating rotors...")
    print("Rotor positions after rotation:", rotor_positions)
    for i in reversed(range(len(rotor_positions) - 1)):
        if rotor_positions[i + 1] > 9:  # Check if the right rotor completed a revolution
            rotor_positions[i + 1] = 0  # Reset the right rotor
            rotor_positions[i] += 1  # Rotate the next rotor to the left
            print("Rotor positions after reset:", rotor_positions)

def encrypt_sequence(sequence, selected_rotors_indices, starting_positions):
    # Extract the selected rotors based on indices provided
    selected_rotors = [rotors[i] for i in selected_rotors_indices]
    rotor_positions = starting_positions[:]  # Copy to avoid modifying the original starting positions
    encrypted_sequence = ""

    for digit in sequence:
        # Encrypt each digit in the sequence
        encrypted_digit = encrypt_digit(digit, rotor_positions, selected_rotors)
        encrypted_sequence += encrypted_digit
        # Rotate rotors after encrypting each digit
        rotate_rotors(rotor_positions)

    return encrypted_sequence


sequence = "1234567890"  # The sequence to be encrypted
selected_rotors_indices = [0, 1, 2]  # Indices of the rotors selected for the encryption
starting_positions = [0, 0, 0]  # Starting positions of the selected rotors

# Encrypt the sequence
encrypted_sequence = encrypt_sequence(sequence, selected_rotors_indices, starting_positions)

print(f"Encrypted sequence: {encrypted_sequence}")
With the code changes I have implemented, using your debugging strategy, I have been able to get the same output from the input, albeit with a slight quirk:

Output:
Forward - Digit after rotor 0 : 1 Forward - Digit after rotor 1 : 1 Forward - Digit after rotor 2 : 1 Reverse - Digit after rotor 2 : 1 Reverse - Digit after rotor 1 : 1 Reverse - Digit after rotor 0 : 1 Rotating rotors... Rotor positions after rotation: [0, 0, 1] Forward - Digit after rotor 0 : 2 Forward - Digit after rotor 1 : 2 Forward - Digit after rotor 2 : 1 Reverse - Digit after rotor 2 : 2 Reverse - Digit after rotor 1 : 2 Reverse - Digit after rotor 0 : 2 Rotating rotors... Rotor positions after rotation: [0, 0, 2] Forward - Digit after rotor 0 : 3 Forward - Digit after rotor 1 : 3 Forward - Digit after rotor 2 : 1 Reverse - Digit after rotor 2 : 3 Reverse - Digit after rotor 1 : 3 Reverse - Digit after rotor 0 : 3 Rotating rotors... Rotor positions after rotation: [0, 0, 3] Forward - Digit after rotor 0 : 4 Forward - Digit after rotor 1 : 4 Forward - Digit after rotor 2 : 1 Reverse - Digit after rotor 2 : 4 Reverse - Digit after rotor 1 : 4 Reverse - Digit after rotor 0 : 4 Rotating rotors... Rotor positions after rotation: [0, 0, 4] Forward - Digit after rotor 0 : 5 Forward - Digit after rotor 1 : 5 Forward - Digit after rotor 2 : 1 Reverse - Digit after rotor 2 : 5 Reverse - Digit after rotor 1 : 5 Reverse - Digit after rotor 0 : 5 Rotating rotors... Rotor positions after rotation: [0, 0, 5] Forward - Digit after rotor 0 : 6 Forward - Digit after rotor 1 : 6 Forward - Digit after rotor 2 : 1 Reverse - Digit after rotor 2 : 6 Reverse - Digit after rotor 1 : 6 Reverse - Digit after rotor 0 : 6 Rotating rotors... Rotor positions after rotation: [0, 0, 6] Forward - Digit after rotor 0 : 7 Forward - Digit after rotor 1 : 7 Forward - Digit after rotor 2 : 1 Reverse - Digit after rotor 2 : 7 Reverse - Digit after rotor 1 : 7 Reverse - Digit after rotor 0 : 7 Rotating rotors... Rotor positions after rotation: [0, 0, 7] Forward - Digit after rotor 0 : 8 Forward - Digit after rotor 1 : 8 Forward - Digit after rotor 2 : 1 Reverse - Digit after rotor 2 : 8 Reverse - Digit after rotor 1 : 8 Reverse - Digit after rotor 0 : 8 Rotating rotors... Rotor positions after rotation: [0, 0, 8] Forward - Digit after rotor 0 : 9 Forward - Digit after rotor 1 : 9 Forward - Digit after rotor 2 : 1 Reverse - Digit after rotor 2 : 9 Reverse - Digit after rotor 1 : 9 Reverse - Digit after rotor 0 : 9 Rotating rotors... Rotor positions after rotation: [0, 0, 9] Forward - Digit after rotor 0 : 0 Forward - Digit after rotor 1 : 0 Forward - Digit after rotor 2 : 1 Reverse - Digit after rotor 2 : 0 Reverse - Digit after rotor 1 : 0 Reverse - Digit after rotor 0 : 0 Rotating rotors... Rotor positions after rotation: [0, 0, 10] Rotor positions after reset: [0, 1, 0] Encrypted sequence: 1234567890
For some reason, after the input passes through "rotor 2" on the forward pass, it always comes out as 1. Yet, that is always resolved and I am getting the correct output. Still, I am wondering why that occurs and if it is a cause for further debugging.



(Mar-28-2024, 04:44 AM)bowlofred Wrote: Think of it this way. Imagine you have a very simplified rotor:
[0, 1, 2, 3, 4]

This is five straight lines. 0->0, 1->1 etc...

What happens when the rotor is moved one click down? Nothing at all. The 0->0 line is now the 1->1 line, and the previous 4->4 line becomes the new 0->0 line. This "null" rotor doesn't change anything when it is moved.

What happens in your program? You advance the input, but not the output. If your rotor is in postion 1, then you run:
            adjusted_input = (digit + rotor_positions[i]) % 10
            digit = rotor[adjusted_input]
If we pass in digit=0 to this, we get adjusted_input = 1 and then the new digit becomes 1. This is incorrect.



You might consider testing your program with a set of null rotors (each is list(range(9))), and not using the reflector. If your code is correct, you should get the input string back without encryption.
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 819 Dec-14-2023, 10:42 AM
Last Post: Larz60+
Question Numeric Anagrams - Count Occurances monty024 2 1,530 Nov-13-2021, 05:05 PM
Last Post: monty024
  How to get datetime from numeric format field klllmmm 3 2,033 Nov-06-2021, 03:26 PM
Last Post: snippsat
  Extract continuous numeric characters from a string in Python Robotguy 2 2,677 Jan-16-2021, 12:44 AM
Last Post: snippsat
  How to calculate column mean and row skip non numeric and na Mekala 5 5,005 May-06-2020, 10:52 AM
Last Post: anbu23
  Alpha numeric element list search rhubarbpieguy 1 1,813 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  convert a character to numeric and back Skaperen 2 2,141 Jan-28-2020, 09:32 PM
Last Post: Skaperen
  are numeric types passed by value or reference? rudihammad 4 2,663 Nov-19-2019, 06:25 AM
Last Post: rudihammad
  'Age' categorical (years -months -days ) to numeric Smiling29 4 2,968 Oct-17-2019, 05:26 PM
Last Post: Smiling29
  how to do a numeric sort Skaperen 11 5,012 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