[Python] Coding enigma algorithm, but suddenly stops working

Discussão em 'Python' iniciado por Stack, Março 19, 2025.

  1. Stack

    Stack Membro Participativo

    I'm having a problem with my enigma encoding, since after a certain amount of encoded characters the machine stops returning the correct characters. (for example when inputting the alfabet 3 times (which is 78 characters) the code is different from online enigma encryptors, but when inputting 78 capital A's the code is the same as the online versions) My code starts with the Enigma function at the bottom

    def rotate(string):
    return string[1:] + string[0]
    def rotor_rotations(rotors,alfators, notches):
    rotors[2] = rotate(rotors[2])
    alfators[2] = rotate(alfators[2])
    if notches[2] == rotors[2][-1]:
    rotors[1] = rotate(rotors[1])
    alfators[1] = rotate(alfators[1])
    if notches[1] == rotors[1][-1]:
    rotors[0] = rotate(rotors[0])
    alfators[0] = rotate(alfators[0])
    return rotors, alfators
    def rotor(text, rotors, alfators, ukw, notches):
    alfabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    retext = ''
    for letter in text:
    rotors, alfators = rotor_rotations(rotors,alfators, notches)
    index = alfabet.index(letter)
    for i in range(2, -1, -1):
    letter = rotors[index]
    index = alfators.index(letter)
    letter = ukw[0][index]
    index = rotors[0].index(letter)
    for i in range(1, 3):
    letter = alfators[index]
    index = rotors.index(letter)
    letter = alfabet[index]
    retext += letter
    return retext, rotors, alfators
    def enigma(text):
    rotors =['EKMFLGDQVZNTOWYHXUSPAIBRCJ','AJDKSIRUXBLHWTMCQGZNPYFVOE','BDFHJLCPRTXVZNYEIWGAKMUSQO']
    alfators = ['ABCDEFGHIJKLMNOPQRSTUVWXYZ']*3
    ukw = ['YRUHQSLDPXNGOKMIEBFZCWVJAT']
    notches = ['H','K','K']
    plugboard_settings = {}
    text = text.split(' ')
    encrypted_text = ''
    for piece in text:
    first = plugboard(piece, plugboard_settings)
    second, rotors, alfators = rotor(first, rotors, alfators, ukw, notches)
    encrypted_text += plugboard(second,plugboard_settings) + ' '
    return encrypted_text


    The code above is my full algorithm, i left out the plugboard algorithm since there arent any problems with that one. Alfators are the alfabets that rotate together with the normal rotors. Can someone please find my mistake? i think it is somewhere in the rotor rotations.

    Continue reading...

Compartilhe esta Página