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

时间:2015-12-07 18:01:53

标签: python keyword vigenere

此代码一直工作到第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))

1 个答案:

答案 0 :(得分:0)

您正在尝试获取明文索引而不是plaintextchar。也许这会奏效:

ciphercharindexvalue = alphabet.index(keylist[keyincrement]) + alphabet.index(plaintextchar)