Use Diffie-Hellman 键 exchange
Diffie-Hellman allows two parties 到 establish shared secret 遍历不安全通道无 ever transmitting secret itself. foundation 的 TLS 键 exchange.
- 1
Diffie-Hellman 工作在 cyclic 组: 两者 parties 同意开 generator g 和素数 p. 每个 picks secret random exponent (, b), sends g^ 和 g^b 公共, 和 computes (g^b)^ = (g^)^b = g^(ab) as shared secret. eavesdropper sees g, p, g^, g^b 但 cannot 计算 g^(ab) 无 solving discrete logarithm 问题.
- 2 素数生成器 →
生成 a safe prime p (p = 2q + 1 where q is also prime). For DH, use a 2048-bit or larger prime. The prime must be shared and public — it can be a well-known group (RFC 7919) or generated fresh.
- 3 模幂运算计算器 →
计算 g^a mod p where a is your secret random exponent. Send the result to the other party. Receive their g^b mod p and compute (g^b)^a mod p to get the shared secret.
- 4
Use ECDH (Elliptic Curve Diffie-Hellman) 而非经典 DH — 它是 faster 和我们 smaller 关键为 equivalent 安全. P-256 (NIST) 或 Curve25519 是标准 choices. 总是 use ephemeral 关键 (DHE/ECDHE) 为 forward secrecy 在 TLS.
常见问题
- 什么是 forward secrecy?
- 每个会话我们 fresh ephemeral 键对 该是 discarded 之后会话. 如果长-项键 是稍后 compromised, past 会话关键 cannot 是 recovered because ephemeral 关键否 longer 存在. TLS 1.3 mandates forward secrecy.
- DH vs ECDH?
- ECDH 我们 elliptic curve groups 而非 multiplicative groups 取模素数. 它 achieves 相同安全具有 much smaller 关键 (256-位 curve ≈ 3072-位 DH), making 它 faster 和更多高效. Use ECDH 具有 Curve25519 或 P-256.