Python中的Caesar密码(意外错误)

2024-03-29 12:55:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须用凯撒密码加密用户提供的明文。将每个纯文本字符转换为其ASCII(整数)值并存储在列表中。 我做过这样的事

print("This program uses a Caesar Cipher to encrypt a plaintext message using the encryption key you provide.")
plaintext = input("Enter the message to be encrypted:")
plaintext = plaintext.upper()
n = eval(input("Enter an integer for an encrytion key:"))
ascii_list = []

# encipher
ciphertext = ""
for x in range(len(plaintext)):
    ascii_list[x] = plaintext (ascii_list) + n %26
    print()

但错误如下所示:

   TypeError: 'str' object is not callable

我希望结果出来:

This program uses a Caesar Cipher to encrypt a plaintext message using the encryption key you provide.
Enter the message to be encrypted: Boiler Up Baby!
Enter an integer for an encrytion key: 1868
The fully encoded message is: CWOTFZ&]QCHICa'

我试过很多不同的方法,但结果都没有出来。你知道吗


Tags: thetokeyanmessageforasciithis
1条回答
网友
1楼 · 发布于 2024-03-29 12:55:31

您需要将初始字符解析为数字,向它们添加键,然后将它们解析回字符。你知道吗

在代码中ascii_list[x]必须更改为ascii_list.append(),因为您引用的索引不存在。而且plaintext不是您可以调用的函数,它只是大写的初始消息。你知道吗

您可以这样做:

for x in range(len(plaintext)):
    ascii_list.append(chr(ord(plaintext[x]) + n))
print(ascii_list)

注意: 您提供的输入/输出(in:Boiler Up Baby!,out:CWOTFZ&]QCHICa')不是典型的Caesar密码,因为有些字母会变成符号,符号也会被编码。使用此解决方案只会将键上移,这意味着例如Z永远不会变成A。如果您需要合适的Caesar密码解决方案,您可能需要考虑以下问题:Caesar Cipher Function in Python

相关问题 更多 >