最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Coding enigma algorithm, but suddenly stops working - Stack Overflow

programmeradmin2浏览0评论

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[i][index]
            index = alfators[i].index(letter)
        letter = ukw[0][index]
        index = rotors[0].index(letter)
        for i in range(1, 3):
            letter = alfators[i][index]
            index = rotors[i].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.

发布评论

评论列表(0)

  1. 暂无评论