Python中的Caesar Cipher(意外错误) [英] Caesar Cipher in Python (unexpected error)

查看:61
本文介绍了Python中的Caesar Cipher(意外错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用Caesar Cipher加密用户提供的纯文本.将每个纯文本字符转换为其ASCII(整数)值并存储在列表中.我已经做到了

  print(此程序使用凯撒密码通过您提供的加密密钥对纯文本消息进行加密.")plaintext = input(输入要加密的消息:")plaintext = plaintext.upper()n = eval(input(输入加密密钥的整数:"))ascii_list = []#加密ciphertext ="对于范围内的x(len(纯文本)):ascii_list [x] =纯文本(ascii_list)+ n%26打印() 

但是错误显示如下:

  TypeError:"str"对象不可调用 

我希望结果出来:

 此程序使用凯撒密码通过您提供的加密密钥对纯文本消息进行加密.输入要加密的消息:沸腾宝贝!输入一个整数作为加密密钥:1868完全编码的消息是:CWOTFZ&] QCHICa' 

我尝试了许多不同的方法,但结果没有出来.

解决方案

您需要将初始字符解析为数字,向其添加键,然后再将其解析为字符.

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

您可以执行以下操作:

<范围> <代码>用于x范围(len(纯文本)):ascii_list.append(chr(ord(plaintext [x])+ n))打印(ascii_list)

注意:您提供的输入/输出(输入: Boiler Up Baby!,输出: CWOTFZ&] QCHICa')不是典型的凯撒密码,因为某些字母变成了符号,符号也被编码.使用此解决方案只会将键向上移动,这意味着例如 Z 永远不会成为 A .如果您需要适当的Caesar密码解决方案,则可能需要查看以下问题: Python中的Caesar密码函数

I have to encrypt a user-provided plaintext using Caesar Cipher. Converting each plaintext character to its ASCII (integer) value and store in a list. I have done like this

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()

But error appears as like this:

   TypeError: 'str' object is not callable

I want the result to come out:

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'

I have tried so many different ways but the result does not come out.

解决方案

You need to parse the initial characters to numbers, add the key to them and then parse them back to characters.

In your code ascii_list[x] must be changed to ascii_list.append() because you are referencing an index that does not exist. Also plaintext is not a function that you can call, it is just your initial message in uppercase.

You can do this:

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

Note: The input/output (in:Boiler Up Baby!, out:CWOTFZ&]QCHICa') you provided is not typical Caesar cipher as some of the letters turn into symbols and also the symbols are encoded as well. Using this solution will only shift the keys up, meaning that for example Z will never become A. If you need proper Caesar cipher solution you might want to look at this question: Caesar Cipher Function in Python

这篇关于Python中的Caesar Cipher(意外错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆