小编jef*_*ran的帖子

pycryptodome:MAC检查失败(使用decrypt_and_verify)

我正在 Python 3.6 中使用 Pycryptodome 开发一个加密程序 我试图加密一个文件,然后解密它并验证 MAC 标签。当我验证它时,会抛出错误

import os
from Crypto.Cipher import AES
Run Code Online (Sandbox Code Playgroud)

围脖 Cryptodome 进口

aad = b'any thing'
nonce = b'\xde\xe2G\xca\xe8Lq9\xeb\x8b\x84\xe7'
key = b'\xde\xe9\xad\xe9\x14_\x07\x1aq7C\\\xd7\x9c\xae\xfcJ\x1c%\xec\xe6C\xbe\xf0eO\xfaJ1\x08\x0c\xae'
Run Code Online (Sandbox Code Playgroud)

我将随机数和密钥设置为常量只是为了首先开始工作。然后我将为每个文件使用唯一的 nonce = get_random_bytes(12) 。

def encrypt(filename):
    chunksize = 64 * 1024
    outputFile = "(encrypted)" + filename
    filesize = str(os.path.getsize(filename))
    cipher = AES.new(key, AES.MODE_GCM, nonce)
    cipher.update(aad)
    with open(filename, 'rb') as infile:
        with open(outputFile, 'wb') as outfile:
            outfile.write(filesize.encode('utf-8'))

            while True:
                chunk = infile.read(chunksize)

                if len(chunk) == 0:
                    break
                elif len(chunk) % 16 != …
Run Code Online (Sandbox Code Playgroud)

cryptography aes python-3.x aes-gcm pycryptodome

5
推荐指数
1
解决办法
2805
查看次数

标签 统计

aes ×1

aes-gcm ×1

cryptography ×1

pycryptodome ×1

python-3.x ×1