python - Python 3 XChaCha20 测试向量适用于加密但解密阶段失败
问题描述
我使用以下向量在 python 中使用 Poly1305 的 AEAD 测试 XChaCha20 加密:
矢量图:
https://datatracker.ietf.org/doc/html/draft-arciszewski-xchacha-03#appendix-A.3
pycryptodome:
https://pycryptodome.readthedocs.io/en/latest/src/cipher/chacha20_poly1305.html
草稿使用 HEX 作为测试向量,如果您真的需要检查我是否使用此服务进行转换:
import json
from base64 import b64encode
from base64 import b64decode
from Crypto.Cipher import ChaCha20_Poly1305
from Crypto.Random import get_random_bytes
#nonce_xchacha20 = get_random_bytes(24)
nonce_xchacha20 = b64decode("QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZX")
#header = b"header"
header = b64decode("UFFSU8DBwsPExcbH")
plaintext = b"Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it."
#key = get_random_bytes(32)
key = b64decode("gIGCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJmam5ydnp8=")
cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce_xchacha20)
cipher.update(header)
ciphertext, tag = cipher.encrypt_and_digest(plaintext)
jk = [ 'nonce', 'header', 'ciphertext', 'tag' ]
jv = [ b64encode(x).decode('utf-8') for x in (cipher.nonce, header, ciphertext, tag) ]
result = json.dumps(dict(zip(jk, jv)))
print(result)
# We assume that the key was securely shared beforehand
try:
b64 = json.loads(result)
jk = [ 'nonce', 'header', 'ciphertext', 'tag' ]
jv = {k:b64decode(b64[k]) for k in jk}
cipher = ChaCha20_Poly1305.new(key=key, nonce=jv['nonce'])
cipher.update(jv['header'])
plaintext = cipher.decrypt_and_verify(jv['ciphertext'], jv['tag'])
print("The message was: " + plaintext)
except (ValueError, KeyError):
print("Incorrect decryption")
print("sanity check if key values are the same: ")
print(b64encode(jv['nonce']))
print(b64encode(jv['header']))
print(b64encode(jv['ciphertext']))
print(b64encode(jv['tag']))
如果测试向量根据 IETF 草案正确加密,为什么我的解密阶段会失败?
{"nonce": "AAAAAFBRUlNUVVZX", "header": "UFFSU8DBwsPExcbH", "ciphertext": "vW0XnT6D1DuVdleUk8DpOVcqFwAlK/rMvtKQLCE5bLtzHH8bC0qmRAvzqC9O2n45rmTGcIxUwhbLlrcuEhO0Ui+Mm6QNtdlFsRtpuYLBu54/P6wrw2lIj3ayODVl0//5IflmTJdjfal2iBL2FcaLE7Uu", "tag": "wIdZJMHHmHlH3q/YeArPSQ=="}
Incorrect decryption
sanity check if key values are the same:
b'AAAAAFBRUlNUVVZX'
b'UFFSU8DBwsPExcbH'
b'vW0XnT6D1DuVdleUk8DpOVcqFwAlK/rMvtKQLCE5bLtzHH8bC0qmRAvzqC9O2n45rmTGcIxUwhbLlrcuEhO0Ui+Mm6QNtdlFsRtpuYLBu54/P6wrw2lIj3ayODVl0//5IflmTJdjfal2iBL2FcaLE7Uu'
b'wIdZJMHHmHlH3q/YeArPSQ=='
当我将字节数组转换回 base64 时,它们仍然与 JSON 输出匹配。因此,从 JSON 中读取我的密钥值进行解密是正确的。
错误在哪里?我确实使用了提供 pycryptodome 的网站上的代码示例,并且加密已正确完成。它应该解密就好了。
解决方案
如果您在该行中替换,解密将正确完成
jv = [ b64encode(x).decode('utf-8') for x in (cipher.nonce, header, ciphertext, tag) ]
cipher.nonce
的表达式nonce_xchacha20
。该错误会导致在 JSON 中提供不正确的随机数。
似乎cipher.nonce
只能用于确定随机生成的随机数(如果在实例化密码时未指定显式随机数,则会生成随机随机数,s. here)。
第二个(微不足道的)变化即将到来
print("The message was: " + plaintext)
必要的。此处必须执行 UTF8 解码,即plaintext
必须替换为plaintext.decode('utf8')
.
在您的第一篇文章中,AAD 也设置不正确。但这已在此期间得到纠正。
通过这两个更改,代码,尤其是解密,可以在我的机器上运行。
推荐阅读
- javascript - 下拉选项 - 重定向或显示文本
- c - 如何在正在运行的进程的 IAT 中挂钩 Windows API 函数并将其替换为虚拟函数?
- react-native - 无法使用 Context API React Native 检索数据
- sql - 通过关系记录计数获取记录的 Sql 查询
- swift - 使用自动布局的 UIToolBar 布局约束问题
- javascript - 如何在两个独立标签之间获取 HTML 元素
- graphql - 所需类型“Int!”的 GraphQL“$id” 没有提供
- google-cloud-platform - 如何从 Google 云调度程序定期触发 Google Cloud Build 作业
- php - Doctrine 在水合过程中添加了额外的查询,导致“正常”一对一和自引用关系出现 n+1 问题
- wordpress - 批量删除某些分类的所有术语(Wordpress)