python - 尝试将字符串加密为 ASCII

标签 python encryption

这是我不确定哪里出错了:

import sys
message = input("enter message here:")
Key = input("enter a key from 1-100:")

for Letter in message:
        Char = ord(Letter)
        if (Char + Key) < 32:
            encryptedChar = ((Char - Key) + 127) - 32
        else:
            encryptedChar = (Char - Key)
            sys.stdout.write(chr(encryptedChar))

            print(encryptedChar,end=" ")

我目前收到错误:

TypeError: unsupported operand type(s) for +: 'int' and 'str')

最佳答案

Key = int(input("enter a key from 1-100:"))

input是一个字符串,需要转换成一个整数。

转换为 int 后,您的代码运行良好:

enter message here:foobar
enter a key from 1-100:10
\92e101e101X88W87h104

关于python - 尝试将字符串加密为 ASCII,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26918780/

相关文章:

java - 加密JSP中的隐藏变量

c# - 在 C# 中导入和导出 RSA key

python - 将元组转换为嵌套列表的索引

python - 如何将 3 维张量绘制为具有不同颜色的管

python - 使用字典的 'get' 方法来选择大小写。为什么它总是选择错误的东西?

javascript - 是否有用于 Javascript 和 Rails 的库来使用 key 加密和解密?

python - 如何在 Python3 中解压缩使用 PKZIP 以外的算法加密的文件?

python - 函数自行运行,但不在类中运行

python - 如何将seaborn regplot scattplot更改为lineplot?

algorithm - 我的 RSA 算法只适用于非常短的单词