Vigenere python - 未找到子字符串错误

问题描述 投票:0回答:1

此代码一直运行到第31行。然后出现错误ciphercharindexvalue = alphabet.index(keylist[keyincrement]) + alphabet.index(plaintext) ValueError: substring not found。我不确定为什么会这样,你能帮帮我吗?我试图使用此代码中的关键字加密代码。我可以使用数字进行加密[如开头所示]但我现在想使用关键字来创建“Vigenere密码”。

这是完整的代码:

eord = input("Would you like to encrypt or decrypt? [e/d]: ")
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012346789"
string = ''

if eord == 'e':
    texte = input ("Type your word to encrypt: ")
    key1 = int(input("Choose a key: "))
    for letter in texte:
        number = (ord(letter)) + (key1)
        letter = (chr(number))
        string = (str(string)) + (str(letter))
    print (string)
    keyword = input ("Would you like to encrypt your code further? [Y/N]: ")



    if keyword == "Y" or "y":
        plaintext = input("Please enter plain text: ")
        key = input("Please enter a keyword: ")
        keylist = []
        keylength = 0
        while keylength < len(plaintext):
            for char in  key:
                if keylength < len(plaintext):
                    keylist.append(str(char))
                    keylength = keylength + 1
        completeciphertext = []
        ciphercharindexvalue = 0
        keyincrement = 0
        for plaintextchar in plaintext:
            ciphercharindexvalue = alphabet.index(keylist[keyincrement]) + alphabet.index(plaintext)
            while ciphercharindexvalue >25:
                ciphercharindexvalue = ciphercharindexvalue - 26
            completeciphertext.append(alphabet[ciphercharindexvalue])
            keyincrement = keyincrement + 1
        print (''.join(completecipertext))
python keyword vigenere
1个回答
0
投票

你试图得到明文索引而不是plaintextchar。也许这会奏效:

ciphercharindexvalue = alphabet.index(keylist[keyincrement]) + alphabet.index(plaintextchar)
© www.soinside.com 2019 - 2024. All rights reserved.