1

我试图检索数据库中的加密值,并解密它们。但这对我不起作用。这是我试图解密的来自 sqlite3 的记录:

[(b'gAAAAABg5EBDnm6LZdHNzzjnDlFcA80SeS1s-wXhKx_opwOm0vSNsfcxheZC4abQ-qvifmxxbCT8gGy2O1dz-76gx3_ALVmeZQ==', b'gAAAAABg5EBD1k2kpmYaAI0PNfjHondg5mB9RwxoIxVqAkfZcAeYrK-H4oqyYjV9WcHFN0CF7Xf0dN2rAXKrkAq-myyfNUplSg==', b'gAAAAABg5EBDJrUQazdiAdf4yejxIwzZHSln-hAcYLkh27-7HO5gE8XjHZjUz3Y2-lHoDIINSAzUt10J93eZ2EeCNUGA1mVrgw==', b'gAAAAABg5EBDp5znqWT87IMASAjDuhMtB7uMV2rezOEQzy_O4ka8R8Dn0fCkZhajVBIQ3S0-MQiM2Mq0qS8JbKCD_rZhbaFPcA==', b'gAAAAABg5EBDZ3YdqhKFRZWQeKP3VUvaVWp-pmSub2aZtQGIpirZ6Pi9zplxdqCindAOHJIQrlcjAr_FG_vnH8U89_bx7FjX2Q==', b'gAAAAABg5EBDjIcwfpyMYXxoEK9XZjJEDyFM3sWfpM61hgAIOf4cNTj1etS1nCMsLIZtKIrF5wSRkmzP_tiXyDolisVkjr6uuA=='), ('gAAAAABg5EDjNQV_tNHuRRkZvdLDfnI20iGZ0Cpzv--1Xct0brLjCOlqnt0ZOU0AYe-tbQ390yIF2B-xlDGuP1-isAtgkWtGTQ==', 'gAAAAABg5EDjC84Zs7HPm1zPe9O2sWNX_HSf3pZeyivmqaPC-VYu5qely818-srsNOtVYtNoGCineThtqQuEC7vjteYV3sHIjA==', 'gAAAAABg5EDj-rUfVVvzObgt4HcgUr7UczgKTrdQyim-ZIY9AeFFGpreZeUS0G9ojkqX_o1AeDyUkcZ4mKIVwoK1dSOINPcp1Q==', 'gAAAAABg5EDjRwttFLM-AhzcFPsKX_cZv9BXnZd__wUH2fRudsFCTwwGnQxhWgxNjNN42zUs_ig6iVmLOJVZRJWno5LzRNQu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5HIvcLXSI-SZnLrwsO0xA3pp6PvLSJQ==')]

这是代码:

key = Fernet.generate_key()

fernet = Fernet(key)
conn = sqlite3.connect('database.db')
c = conn.cursor()
def get_clients():
    arr = []
    retrieved_vals = ""
    c.execute("SELECT * FROM client")
    print(c.fetchall())
    arr = c.fetchall()
    element2 = ""
  
    for element in arr[0]:
        print(fernet.decrypt(element).decode())
        

但是我得到了这些错误列表:

Traceback (most recent call last):
  File "C:\Users\d\AppData\Local\Programs\Python\Python38\lib\site-packages\cryptography\fernet.py", line 119, in _verify_signature
    h.verify(data[-32:])
  File "C:\Users\d\AppData\Local\Programs\Python\Python38\lib\site-packages\cryptography\hazmat\primitives\hmac.py", line 74, in verify
    ctx.verify(signature)
  File "C:\Users\d\AppData\Local\Programs\Python\Python38\lib\site-packages\cryptography\hazmat\backends\openssl\hmac.py", line 75, in verify
    raise InvalidSignature("Signature did not match digest.")
cryptography.exceptions.InvalidSignature: Signature did not match digest.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\d\OneDrive\Bureaublad\Assignment8\CDMS.py", line 55, in <module>
    get_clients()
  File "C:\Users\d\OneDrive\Bureaublad\Assignment8\CDMS.py", line 42, in get_clients
    print(fernet.decrypt(element).decode())
  File "C:\Users\d\AppData\Local\Programs\Python\Python38\lib\site-packages\cryptography\fernet.py", line 80, in decrypt
    return self._decrypt_data(data, timestamp, time_info)
  File "C:\Users\d\AppData\Local\Programs\Python\Python38\lib\site-packages\cryptography\fernet.py", line 137, in _decrypt_data
    self._verify_signature(data)
  File "C:\Users\d\AppData\Local\Programs\Python\Python38\lib\site-packages\cryptography\fernet.py", line 121, in _verify_signature
    raise InvalidToken
cryptography.fernet.InvalidToken

我不知道我做错了什么

4

2 回答 2

1

解密期间不能生成新密钥。

key = Fernet.generate_key()

您必须使用与加密相同的密钥。

于 2021-10-01T19:50:29.857 回答
0

由于它是基于密钥的对称加密,因此您必须使用用于加密这些数据的相同密钥。否则你将无法解密它。

于 2021-10-01T20:01:48.480 回答