python-3.x 我正尝试使用密码学对字典进行加密

l3zydbqr  于 11个月前  发布在  Python
关注(0)|答案(1)|浏览(1211)

我正试图使一个密码锁,需要输入和加密的数据,也正试图保存到字典的数据和加密的价值观只。

from cryptography.fernet import Fernet 
key = Fernet.generate_key() 
f = Fernet(key)
loc = {}  # an empty dict that stores the keys and values
title = input('enter your title: ')  # getting user title as key
secret = input('enter your secret: ') # getting secret as value to be encrypted
loc[title] = secret.encode()  #turning the value into a byte for encryption
print(f.encrypt(secret)) # but am having an error trying to get through

字符串
任何帮助将不胜感激。谢谢。

ttygqcqt

ttygqcqt1#

有一段时间了,但是...
我会加密整个dict,但在此之前,你需要将dict转换为字节:

import json
from cryptography.fernet import Fernet 
key = Fernet.generate_key() 
f = Fernet(key)
loc = {}
title = input('enter your title: ')
secret = input('enter your secret: ')
loc[title] = secret

encrypt_this = json.loads(loc).encode('utf-8')
encrypted = cryptography.fernet.Fernet(key).encrypt(encrypt_this)

字符串
在另一端,你会解密它并将字节转换回dict:

decrypt_this = cryptography.fernet.Fernet(key).decrypt(encrypted)
decrypted = json.loads(decrypt_this.decode('utf-8'))

相关问题