English 中文(简体)
正在试图用加密法对一名词典进行加密
原标题:am trying to encrypt a dictionary using cryptography

I m trying to make a passowrd locker, that takes input and encrypt the data and also am trying to save the data into a dictionary and encrypt the values only.

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

希望得到任何帮助。 感谢。

问题回答

......

我将加密整个判词,但在此之前,你必须把dict改成tes子:

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)

and on the other end, you would decrypt it and convert the bytes back into a dict:

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




相关问题
Get webpage contents with Python?

I m using Python 3.1, if that helps. Anyways, I m trying to get the contents of this webpage. I Googled for a little bit and tried different things, but they didn t work. I m guessing that this ...

What is internal representation of string in Python 3.x

In Python 3.x, a string consists of items of Unicode ordinal. (See the quotation from the language reference below.) What is the internal representation of Unicode string? Is it UTF-16? The items ...

What does Python s builtin __build_class__ do?

In Python 3.1, there is a new builtin function I don t know in the builtins module: __build_class__(...) __build_class__(func, name, *bases, metaclass=None, **kwds) -> class Internal ...

what functional tools remain in Python 3k?

I have have read several entries regarding dropping several functional functions from future python, including map and reduce. What is the official policy regarding functional extensions? is lambda ...

Building executables for Python 3 and PyQt

I built a rather simple application in Python 3.1 using PyQt4. Being done, I want the application to be distributed to computers without either of those installed. I almost exclusively care about ...

热门标签