Python中的Caesar密码无法解密

Python中的Caesar密码无法解密,python,encryption,caesar-cipher,Python,Encryption,Caesar Cipher,我的加密工作正常,但是当我运行解密代码时,它不工作。当我到达代码的那一部分时,出现了一个错误- cipher2 += cipher[(cipher(A)-key1)%(len(cipher2))] TypeError: 'str' object is not callable 您可以创建一个用于加密和解密的函数 alphabetL = 'abcdefghijklmnopqrstuvwxyz' alphabetC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' number = '0

我的加密工作正常,但是当我运行解密代码时,它不工作。当我到达代码的那一部分时,出现了一个错误-

cipher2 += cipher[(cipher(A)-key1)%(len(cipher2))] TypeError: 'str' object is not callable
您可以创建一个用于加密和解密的函数

alphabetL = 'abcdefghijklmnopqrstuvwxyz'
alphabetC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
number = '0123456789'
space = ' '
Ll = len(alphabetL)
Lc = len(alphabetC)
Ln = len(number)
Lall = Ll + Lc + Ln
msgall = alphabetL + alphabetC + number + space
Question1 = input("Hello, please insert the message you want encrypted: ")
key1 = int(input("Please insert the key you want used [Keep between 1 and 26]: "))

cipher2 = ''

def ceaser_cipher(Question1,key1):
    cipher = ''
    for A in Question1:
        if A in alphabetL:
            cipher += alphabetL[(alphabetL.index(A)+key1)%Ll]
        elif A in alphabetC:
            cipher += alphabetC[(alphabetC.index(A)+key1)%Lc]
        elif A in number:
            cipher += number[(number.index(A)+key1)%Ln]
        elif A in space:
            cipher += space
        else:
            print ("Error, please use abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")

    return cipher


cipher=ceaser_cipher(Question1,key1)
print (cipher)
Question2 = input("Would you like to decrypt the message? [Y/N]: ")
if Question2 == "Y":
    cipher2=ceaser_cipher(cipher,-key1)
    print (cipher2)

问题在于倒数第二行的
密码(A)
部分。
cipher
变量是字符串,不能像函数一样调用。你想调用
.index()
方法吗?我不确定。您会将其更改为什么?您可能想考虑使用而不是对字符串本身调用该方法。这不起作用。对不起,
cipher(A)
对倒数第二行调用的目的是什么?当我运行代码时,会出现错误消息-“NameError:name'raw_input'未定义”。你能给我一些建议吗?@BigTommyVernon这意味着你在使用python-3.x。我已经更新了答案。
alphabetL = 'abcdefghijklmnopqrstuvwxyz'
alphabetC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
number = '0123456789'
space = ' '
Ll = len(alphabetL)
Lc = len(alphabetC)
Ln = len(number)
Lall = Ll + Lc + Ln
msgall = alphabetL + alphabetC + number + space
Question1 = input("Hello, please insert the message you want encrypted: ")
key1 = int(input("Please insert the key you want used [Keep between 1 and 26]: "))

cipher2 = ''

def ceaser_cipher(Question1,key1):
    cipher = ''
    for A in Question1:
        if A in alphabetL:
            cipher += alphabetL[(alphabetL.index(A)+key1)%Ll]
        elif A in alphabetC:
            cipher += alphabetC[(alphabetC.index(A)+key1)%Lc]
        elif A in number:
            cipher += number[(number.index(A)+key1)%Ln]
        elif A in space:
            cipher += space
        else:
            print ("Error, please use abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")

    return cipher


cipher=ceaser_cipher(Question1,key1)
print (cipher)
Question2 = input("Would you like to decrypt the message? [Y/N]: ")
if Question2 == "Y":
    cipher2=ceaser_cipher(cipher,-key1)
    print (cipher2)