所以我的问题是,我加密了一些密码,把它存储在我的数据库里,就像这样:
"b'bytesandstuff'"
但如果我检索它并尝试解码它,它崩溃了,因为字符串不是一个字节,什么是最简单的方法来解决这个问题?你知道吗
另外,我不认为我需要为此提供代码,但我无论如何都会这样做
你知道吗主.py你知道吗
from passgen import encryptPass, decryptPass
global decpass
encpass = loginquery.get("password")
decpass = decryptPass(encpass)
你知道吗passgen.py公司你知道吗
from cryptography.fernet import Fernet
key = "mySecretKey"
f = Fernet(key)
def decryptPass(rawstring):
decrypted = f.decrypt(rawstring)
converted = decrypted.decode('utf8')
return converted
编辑:我正在使用python3.6.4
编辑2:一条记录如下:RethinkDB Sample Record
由于您有字节作为输入,并且希望将其存储为字符串,因此可以先使用base64.b64encode在base64中对其进行编码,然后在
ascii
中对字符串进行解码,然后再存储到db中。你知道吗问题是为什么你不能简单地做
encrypted_message_as_returned_by_fernet.decode('ascii')
。这是因为bytes type可以包含像b'\0'
这样的空字节,如果在字符串中找到这些空字节,则在某些系统中可能无法正常工作。你知道吗为了安全起见,需要字节的字符串表示形式时,可以随时使用base64。它不是Python特有的。例如,图像是直接包含在html页面中的base64。它叫做data URLs
相关问题 更多 >
编程相关推荐