首页 文章

尝试解密消息但是gettingTypeError:无法将字节连接到str

提问于
浏览
1

我试图解密存储在数据库中的加密数据,但收到类型错误消息:TypeError:无法将字节连接到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)

1 回答

  • 0

    对于二进制数据,例如加密数据或压缩数据,有必要将其存储在二进制字段中,例如Postgres中的 bytea 类型 .

    将它存储在 varchar 字段中意味着某些字节,特别是零值会导致数据丢失 .

相关问题