小编Zak*_*k35的帖子

是否可以为以后的会话保存 fernet 密钥?

我是 python 的新手,我正在开发一个加密文本字符串的程序,然后将其保存到一个文件中。当我在同一个会话中加密然后解密它时,我的程序运行良好。我想做的是:加密文件,然后关闭程序,稍后再返回并解密。我不知道密码学模块是如何工作的,但判断它是如何被称为“密钥”的,我认为这对安全很重要,但我不知道。当我尝试将 fernet 密钥保存到文本文件时,它会显示一条错误消息。当我尝试解密在前一个会话中加密的消息时,它显示 4 个引用加密模块的错误。最后,我想知道是否可以使用加密模块在会话之间解密数据。如果不,有没有其他方法可以完成这项任务?谢谢你的帮助。这是我的代码:

from cryptography.fernet import Fernet
key = Fernet.generate_key()
f = Fernet(key)
sel = input("Would you like to encrypt or decrypt? (1 = encrypt, 2 = decrypt) ")
if sel == 1:
    inp = raw_input("Enter Text: ")  # Type here
    encoded = f.encrypt(inp)
    a, b = encoded[:len(encoded)/2], encoded[len(encoded)/2:]
    print ("YOUR PASSWORD: ")
    print b
    file = open('password.txt', 'w')
    file.write(a)
elif sel == 2:
    inp = raw_input("Enter Password: ")
    file = open('password.txt', 'r')
    a = file.readline() …
Run Code Online (Sandbox Code Playgroud)

python encryption cryptography python-2.7 python-cryptography

7
推荐指数
1
解决办法
4108
查看次数