pur*_*dot 4 python encryption cryptography pycrypto pycryptodome
我正在使用 Python 3 中的 Pycryptodome 开发加密程序。我正在尝试加密(字节)字符串,然后对其进行解密并验证 MAC 标签。当我验证它时,会抛出一个错误。
这是代码:
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
aes_key = get_random_bytes(24)
aes_cipher = AES.new(aes_key, AES.MODE_GCM)
encrypted, MACtag = aes_cipher.encrypt_and_digest(b"A random thirty two byte string.")
# Imagine this is happening somewhere else
new_aes_cipher = AES.new(aes_key, AES.MODE_GCM, nonce=aes_cipher.nonce)
new_aes_cipher.verify(MACtag)
decrypted = new_aes_cipher.decrypt(encrypted)
Run Code Online (Sandbox Code Playgroud)
这是错误:
Traceback (most recent call last):
File "aespractice.py", line 10, in <module>
new_aes_cipher.verify(tag)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/Crypto/Cipher/_mode_gcm.py", line 441, in verify
raise ValueError("MAC check failed")
ValueError: MAC check failed
Run Code Online (Sandbox Code Playgroud)
我查看了文档,在我看来一切都很好。为什么你认为这个程序会这样?任何帮助,将不胜感激。
如果您查看身份验证模式的状态图:
你会看到verify()应该在最后调用,在任何decrypt()发生之后。因此,要么反转调用,要么用组合的decrypt_and_verify().
| 归档时间: |
|
| 查看次数: |
3484 次 |
| 最近记录: |