数学史

迪菲–赫尔曼密钥交换

1976 年,迪菲与赫尔曼展示了双方如何通过公开信道协商出共同密钥。这个玩具版本使用 p = 23 和 g = 5:选择私钥 a 与 b,公开 A 与 B,观察双方算出相同的共享密钥。

7 分钟难度 3/3交互演示区域不展示广告。
Prime p23Generator g5A = g^a mod p8B = g^b mod p4Shared secret2
Alice — private key a = 6
a = 6
g^a mod p = 5^6 mod 23 = 8
public value A = 8
public channel
A = 8p = 23, g = 5B = 4
shared secret = 2 · match
Bob — private key b = 4
b = 4
g^b mod p = 5^4 mod 23 = 4
public value B = 4

Alice: B^a mod p = 4^6 mod 23 = 2 · Bob: A^b mod p = 8^4 mod 23 = 2 · both sides agree

Paint-mixing analogyAlice and Bob each pick a secret color. They mix it with a shared public color and send the mixed result over an open channel. An eavesdropper sees only muddy mixtures. Mixing is easy, but separating the original secret color back out is believed to be hard — that is the one-way idea behind Diffie–Hellman.
Toy numbers onlyWith p = 23 and g = 5 an attacker can try every exponent by brute force in an instant. Real Diffie–Hellman uses primes with hundreds or thousands of bits, so that the discrete logarithm is infeasible. This demo is for understanding the exchange, not for protecting data.

The public channel exposes p = 23, g = 5, A, and B, but never a or b. Alice raises B to her secret a; Bob raises A to his secret b. Modular exponentiation gives both the same value, because (g^b)^a = (g^a)^b = g^(ab) mod p. Change either private key and the shared secret changes for both sides at once, while an attacker would need to solve a discrete logarithm.

你看到的是什么

1

Alice 与 Bob 公开约定素数 p 和生成元 g。Alice 保留秘密指数 a,并公开 A = g^a mod p。

2

Bob 保密 b,公开 B = g^b mod p。公开的 A 和 B 都不会泄露各自的私有指数。

3

Alice 计算 B^a mod p,Bob 计算 A^b mod p,两者都等于 g^(ab) mod p,于是他们共享一个从未在信道上传输的秘密。

为什么重要

迪菲–赫尔曼是第一个实用的公钥密钥交换方案,支撑着 TLS、SSH 和 VPN。它把“如何共享秘密”这一古老问题,转化为对计算困难性的假设。

复杂度与细节

每次幂运算需要 O(log e) 次模乘。玩具演示很容易;真实系统需要足够大的素数,使离散对数不可行。

常见问题

窃听者为什么不能直接算出 a 或 b?+

从 A = g^a mod p 反推出 a 就是离散对数问题。对大的素数而言,尽管模幂运算本身很快,但反推被认为是计算上不可行的。

这个演示安全吗?+

不安全。当 p = 23、g = 5 时,所有可能的指数都能瞬间穷举。本演示只用于展示交换过程,真实部署使用巨大的素数并配合认证的密钥交换。