【发布时间】:2019-03-26 09:09:08
【问题描述】:
我想使用 Cryptography 对从用户输入中获得的消息进行加密:
我有以下简单的代码:
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
backend = default_backend()
messages = input("Please, insert messages to encrypt: ")
key = os.urandom(24)
print(key)
cipher = Cipher(algorithms.TripleDES(key), modes.ECB(), backend=backend)
encryptor = cipher.encryptor()
cryptogram = encryptor.update(b"a secret message") + encryptor.finalize()
print(cryptogram)
当我在代码中对带有 b 前缀的消息 "a secret message" 进行硬编码时,它可以正常工作。 我想做的是使用 messages 变量从用户输入中获取文本。
messages = input("Please, insert messages to encrypt: ")
我尝试了几种方法将字符串类型从输入转换为字节类型并将其传递给 encryptor.update 方法,但对我没有任何作用。
messages = input(b"Please, insert messages to encrypt: ")
cryptogram = encryptor.update(byte, message) + encryptor.finalize()
...
版本:
Python 3.7.0
密码学 2.4
Mac 操作系统
【问题讨论】:
-
搜索起来很简单。下次请寻找重复的问题。这将节省您编写长问题的精力,而我们则无需关闭它。
-
感谢您的建议,抱歉,我找不到。
标签: python python-3.x cryptography