gpt4 book ai didi

python - 如何在python中加密和解密字符串?

转载 作者:IT老高 更新时间:2023-10-28 21:57:59 25 4
gpt4 key购买 nike

我一直在寻找有关如何加密和解密字符串的时间。但大部分都在 2.7 中,使用 3.2 的任何内容都不允许我打印它或将其添加到字符串中。

所以我想做的是以下几点:

mystring = "Hello stackoverflow!"
encoded = encode(mystring,"password")
print(encoded)

jgAKLJK34t3g (a bunch of random letters)

decoded = decode(encoded,"password")
print(decoded)

Hello stackoverflow!

有没有这样做,使用 python 3.X 并且当字符串被编码时它仍然是一个字符串,而不是任何其他变量类型。

最佳答案

在我的 Windows 7 系统和 Python 3.5 上编译所有最常提到的加密库时遇到了麻烦。

这是最终对我有用的解决方案。

from cryptography.fernet import Fernet
key = Fernet.generate_key() #this is your "password"
cipher_suite = Fernet(key)
encoded_text = cipher_suite.encrypt(b"Hello stackoverflow!")
decoded_text = cipher_suite.decrypt(encoded_text)

关于python - 如何在python中加密和解密字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27335726/

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