我正在尝试解密存储在数据库中的加密数据,但收到类型错误消息:
类型错误:无法将字节连接到str
这是我的代码:

    password = b'password shared between Alice and Bob'
    message = b"This is a message for Bob's eyes only"
    kdf = pwhash.argon2i.kdf
    salt = utils.random(pwhash.argon2i.SALTBYTES)
    ops = pwhash.argon2i.OPSLIMIT_SENSITIVE
    mem = pwhash.argon2i.MEMLIMIT_SENSITIVE

    key = kdf(secret.SecretBox.KEY_SIZE, password, salt,opslimit=ops, memlimit=mem)
    box = secret.SecretBox(key)
    nonce = utils.random(secret.SecretBox.NONCE_SIZE)
    encrypted = box.encrypt(message, nonce)

    sql = "INSERT INTO encrypted (datastored) VALUES (%s);"
    cur.execute(sql,(str(encrypted),))
    conn.commit()


    sql = "SELECT  datastored from  encrypted"
    cur.execute(sql)
    row_value = cur.fetchone()[0]

    key_for_decryption = kdf(secret.SecretBox.KEY_SIZE, password,salt, opslimit=ops, memlimit=mem)
    decryption_box = secret.SecretBox(key_for_decryption)
    received = decryption_box.decrypt(row_value)

最佳答案

对于二进制数据,如加密或压缩数据,有必要将其存储在二进制字段中,如Postgres中的bytea类型。
将其存储在varchar字段中意味着某些字节(特别是零值字节)将导致数据丢失。

关于python-3.x - 尝试解密消息,但gettingTypeError:无法将字节连接到str,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51153838/

10-12 18:15