Understand 和 use 流式处理 ciphers 正确
流式处理 ciphers 加密字节-通过-字节 using keystream, offering 速度和 否 padding. Learn 正确 nonce handling 和 fatal mistake 的 nonce reuse.
- 1
流式处理 cipher generates pseudorandom keystream 和 XORs 它具有 plaintext. ciphertext 是 exactly 相同长度 as plaintext — 否 padding needed. 关键规则: 从不 reuse (键, nonce) 对, 或 XOR 的 two ciphertexts reveals XOR 的 two plaintexts.
- 2 RC4 加密器 →
加密 a short message to see the stream cipher property: ciphertext length equals plaintext length. Observe how the output changes completely with a different key.
- 3 RC4 加密器 →
加密 a second message with the SAME key to demonstrate the nonce-reuse catastrophe. XOR the two ciphertexts — the keystream cancels and you get the XOR of the two plaintexts, leaking structural information.
- 4
现代流式处理 ciphers (ChaCha20, AES-CTR, AES-GCM) use explicit nonce 到变化 keystream per 消息. Generate fresh nonce 为每个加密 — 从不 hardcode 它, 从不 reuse 它. Use 已认证加密 (AEAD) 到检测 tampering.
常见问题
- 为什么是 nonce reuse catastrophic?
- 如果 two messages 是 encrypted 具有相同键 和 nonce, C1 XOR C2 = P1 XOR P2. keystream cancels out. attacker 谁 guesses one plaintext recovers 其他. 此 broke WPA2 (KRACK 攻击).
- Stream cipher vs 阻止 cipher — 哪个到 use?
- Use AES-GCM ( 阻止 cipher 在 AEAD 模式) 在最多 cases. Use ChaCha20-Poly1305 开 devices 无 AES hardware. 从不 use raw 流式处理 cipher 无认证.