【问题标题】:Vigenere python - substring not found errorVigenere python - 找不到子字符串错误
【发布时间】:2015-12-07 18:01:52
【问题描述】:

这段代码一直工作到第 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】:

    您正在尝试获取纯文本而不是纯文本字符的索引。也许这会起作用:

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

    【讨论】:

      猜你喜欢
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 2012-08-20
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多