Python. crypto.cipher.blowfish和.AES的输出无法转换为unicode(sqlite需要)或hex

0 投票
1 回答
1046 浏览
提问于 2025-04-16 03:05

好的,我刚接触Python,所以我决定做一个简单的应用程序。这里是我的加密函数:

from Crypto.Cipher import AES
def encPass(login, password):
    keyPhr=os.environ['HOME']+login
    hashObj = hashlib.md5()
    hashObj.update(keyPhr)
    keyPhr=hashObj.hexdigest()
    keyObj=AES.new(keyPhr)
    encPwd=keyObj.encrypt(password+'pssd')
    return encPwd

你可以看到,它获取登录名和密码,并把密码加密,同时绑定到电脑上。

问题是,当我尝试把加密后的密码放入sqlite3并插入到一个表格里时,它显示:

sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.

如果我尝试把加密后的密码放进unicode()或hex()里:

TypeError: hex() argument can't be converted to hex
UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c in position 0: unexpected code byte

我想这可能是因为我还是个新手,但我该怎么办呢?如果用Blowfish代替AES也是一样的情况。

1 个回答

1

你可以使用 buffer,然后把 encPwd 作为一个二进制大对象(blob)插入。

或者你可以用类似 base64 的方法,把你的 encPwd 转换成 ASCII(ASCII 是 UTF8 的一个子集),这样你就可以把它作为字符串插入。

撰写回答