尝试加密/解密时出现 vigenere 密码错误

Posted

技术标签:

【中文标题】尝试加密/解密时出现 vigenere 密码错误【英文标题】:vigenere cipher error when trying to encrypt/decrypt 【发布时间】:2013-11-16 04:54:58 【问题描述】:

我在运行程序时收到此错误:

Traceback (most recent call last):
File "C:/Users/####/PycharmProjects/hw5/Homework5_Cepero.py", line 112, in <module>
decrypt()
File "C:/Users/####/PycharmProjects/hw5/Homework5_Cepero.py", line 78, in decrypt
encrypted.append(stringlist[i] - keylist[i] % 29)
TypeError: list indices must be integers, not str

Process finished with exit code 1

代码如下:

Alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
        "V", "W", "X", "Y", "Z", ",", ".", "-"]

def encrypt():
    count = 0
    i = 0
    output = []
    encrypted = []
    keylist = []
    stringlist = []
    string = raw_input("Please enter a string to encrypt: ")
    string = string.replace(' ', '')
    key = raw_input("Please enter the key you'd like to use: ")
    keylength = len(key)
    stringlength = len(string)
    overlap = stringlength % keylength
    leftovers = key[:overlap]
    random = stringlength - overlap
    random = stringlength / keylength
    key = (int(random) * key) + leftovers

    #convert Key to uppercase
    for i in key:
        number = Alphabet.index(i.upper())
        keylist.append(number)
    #convert String to uppercase
    for i in string:
        number = Alphabet.index(i.upper())
        stringlist.append(number)

    while count < stringlength:
            encrypted.append((stringlist[i] + keylist[i]) % 29)
            count += 1
            i += 1
    for N in encrypted:
        output.append(Alphabet[N])

    string = ''.join(output)
    print" "
    print"Output:"
    print" "
    print "The encoded text is:", string
    print" "


def decrypt():
    count = 0
    i = 0
    output = []
    encrypted = []
    keylist = []
    stringlist = []
    string = raw_input("Please enter a string to decrypt: ")
    string = string.replace(' ', '')
    key = raw_input("Please enter the key you'd like to use: ")
    keylength = len(key)
    stringlength = len(string)
    overlap = stringlength % keylength
    leftovers = key[:overlap]
    random = stringlength - overlap
    random = stringlength / keylength
    key = (int(random) * key) + leftovers

    #convert Key to uppercase
    for i in key:
        number = Alphabet.index(i.upper())
        keylist.append(number)
    #convert String to uppercase
    for i in string:
        number = Alphabet.index(i.upper())
        stringlist.append(number)

    while count < stringlength:
            encrypted.append(stringlist[i] - keylist[i] % 29)
            count += 1
            i += 1
    for N in encrypted:
        output.append(Alphabet[N])

    string = ''.join(output)
    print" "
    print"Output:"
    print" "
    print "The decoded text is:", string
    print" "

# main function
if __name__ == '__main__':

    selection = ''

    while selection != 3:
            selection = raw_input("\n\nEnter 1 to encrypt, 2 to decrypt, or 3 to exit: ")

            if selection == '1':
                    encrypt()
            elif selection == '2':
                    decrypt()
            elif selection == '3':
                    print "Thanks for using my program"
                    exit()

【问题讨论】:

请发布完整的回溯,包括行号。 【参考方案1】:

在此声明中

encrypted.append((stringlist[i] + keylist[i]) % 29)

您正在使用i,它以前用于对字符串进行迭代。

for i in string:

所以,i 仍然是字符串的最后一个字符。您正在使用该字符串来索引列表。这就是python抛出这个错误的原因。

【讨论】:

我明白为什么现在给我这个错误,但老实说我不知道​​如何解决它。 @user2998626 为什么不在while count &lt; stringlength: 之前设置i = 0 解决了这个问题。非常感谢您的帮助。 @user2998626 如果对您有帮助,请考虑接受此答案:) meta.stackexchange.com/a/5235/235416

以上是关于尝试加密/解密时出现 vigenere 密码错误的主要内容,如果未能解决你的问题,请参考以下文章

使用 vigenere 密码加密时出现奇怪的错误

Python 中的 Vigenere 密码

Python Vigenere 代码重复错误

python 简单的加密/解密算法(Vigenere密码)

Vigenere 密码破坏了一些

Vigenere Cipher使用命令行提示