为什么Pycryptodome MAC加密解密JSON文件时校验失败?

Why does Pycryptodome MAC check fail when encrypting and decrypting JSON files?

我正在尝试使用 AES-256 加密某些 JSON 数据,使用以 pbkdf2_sha256 作为密钥的散列密码。我想将数据存储在一个文件中,能够加载、解密、更改、加密、存储并重复。

我将 passlibpycryptodome 库与 python 3.8 一起使用。以下测试发生在 docker 容器内并抛出一个我无法更正的错误

有没有人知道如何改进我的代码(和知识)?

Test.py:

import os, json
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES
from passlib.hash import pbkdf2_sha256

def setJsonData(jsonData, jsonFileName):
    with open(jsonFileName, 'wb') as jsonFile:
        password = 'd'
        key = pbkdf2_sha256.hash(password)[-16:]

        data = json.dumps(jsonData).encode("utf8")
        cipher = AES.new(key.encode("utf8"), AES.MODE_EAX)
        ciphertext, tag = cipher.encrypt_and_digest(data)

        [ jsonFile.write(x) for x in (cipher.nonce, tag, ciphertext) ]

def getJsonData(jsonFileName):
   with open(jsonFileName, 'rb') as jsonFile:
        password = 'd'
        key = pbkdf2_sha256.hash(password)[-16:]

        nonce, tag, ciphertext = [ jsonFile.read(x) for x in (16, 16, -1) ]
        cipher = AES.new(key.encode("utf8"), AES.MODE_EAX, nonce)
        data = cipher.decrypt_and_verify(ciphertext, tag)

        return json.loads(data)


dictTest = {}
dictTest['test'] = 1

print(str(dictTest))
setJsonData(dictTest, "test")

dictTest = getJsonData("test")
print(str(dictTest))

输出:

{'test': 1}
Traceback (most recent call last):
  File "test.py", line 37, in <module>
    dictTest = getJsonData("test")
  File "test.py", line 24, in getJsonData
    data = cipher.decrypt_and_verify(ciphertext, tag)
  File "/usr/local/lib/python3.8/site-packages/Crypto/Cipher/_mode_eax.py", line 368, in decrypt_and_verify
    self.verify(received_mac_tag)
  File "/usr/local/lib/python3.8/site-packages/Crypto/Cipher/_mode_eax.py", line 309, in verify
    raise ValueError("MAC check failed")
ValueError: MAC check failed

研究:

这里的问题是 key = pbkdf2_sha256.hash(password)[-16:] 每次调用都使用新的盐对密钥进行哈希处理。因此,用于加密和解密密文的密码将不同,产生不同的数据,从而无法通过完整性检查。

我将密钥派生函数更改为以下内容:

h = SHA3_256.new()
h.update(password.encode("utf-8"))
key = h.digest()