加密算法基础95 条

ChaCha20 流密码

RFC 8439 定义的流密码:256 位密钥、96 位 nonce、32 位计数器、20 轮 ARX 运算,软件实现性能好,在无 AES 硬件加速的移动与嵌入式设备上有优势。

加密与证书速查安全更新于 2026-10-12
标准RFC 8439
密钥 / nonce256 位 / 96 位(协议层)
计数器32 位
轮数20 轮(10 个双轮)

Python 使用示例

Python 使用示例
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms
import os

key, nonce = os.urandom(32), os.urandom(16)
enc = Cipher(algorithms.ChaCha20(key, nonce), mode=None).encryptor()
ct = enc.update(b"data") + enc.finalize()
裸 ChaCha20 无完整性保护;协议中应使用 ChaCha20-Poly1305(AEAD),并保证同一密钥下 nonce 不重复。

易错点

  • cryptography 库的 ChaCha20 原语接口接收 128 位 nonce(含内部计数器),与协议层 96 位 nonce 写法不同,混用会得到错误结果
  • ChaCha20 与 Salsa20 参数不同,测试向量不可互换

数据来源与参考

  • RFC 8439(ChaCha20 and Poly1305 for IETF Protocols)
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌