gpt4 book ai didi

python - 如何使用加密模块加密变量?

转载 作者:行者123 更新时间:2023-11-28 21:47:08 25 4
gpt4 key购买 nike

我不确定你们是否熟悉密码模块,但我正在尝试加密一个代表字符串的变量。

例如:

string = input('String here')

他们在模块页面上给出的例子是:

from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher_suite = Fernet(key)
cipher_text = cipher_suite.encrypt(b"A really secret message. Not for prying eyes.")
plain_text = cipher_suite.decrypt(cipher_text)

一切都很好,但当我尝试用变量替换“真正 secret 的消息字符串时,它不起作用。”

如果它在引号中,它只打印变量的名称(duh)

如果它没有像这样的引号:cipher_text = cipher_suite.encrypt(bstring),它表示变量未定义(也是 duh)

但是如果我只是把变量放进去,它会给我一个错误:TypeError: data must be bytes.

有什么想法吗?谢谢!

最佳答案

根据 Python documentation ,

bytes and bytearray objects are sequences of integers (between 0 and 255), representing the ASCII value of single bytes

我觉得输入需要这样

a = b"abc"

(注意“b”)。

另一种实现方式是

a = bytes("abc")

关于python - 如何使用加密模块加密变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36958748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com