gpt4 book ai didi

java - SecureRandom 是否会降低伪随机数据的熵?

转载 作者:行者123 更新时间:2023-12-02 08:46:36 24 4
gpt4 key购买 nike

我想知道 Docker 容器中的随机(或伪随机)序列生成,但遇到了另一个有趣的行为。

直接从 /dev/urandom 读取 8000000 字节并使用 ENT 测试结果时给出以下内容:

Entropy = 7.999976 bits per byte.

Optimum compression would reduce the size
of this 8000000 byte file by 0 percent.

Chi square distribution for 8000000 samples is 262.08, and randomly
would exceed this value 36.69 percent of the times.

Arithmetic mean value of data bytes is 127.5337 (127.5 = random).
Monte Carlo value for Pi is 3.139911785 (error 0.05 percent).
Serial correlation coefficient is -0.000101 (totally uncorrelated = 0.0).

但是如果生成 1000000 个 DES key ,则输出 ENT给出以下内容:

Entropy = 6.999990 bits per byte.

Optimum compression would reduce the size
of this 8000000 byte file by 12 percent.

Chi square distribution for 8000000 samples is 8000217.63, and randomly
would exceed this value less than 0.01 percent of the times.

Arithmetic mean value of data bytes is 127.4870 (127.5 = random).
Monte Carlo value for Pi is 3.145497786 (error 0.12 percent).
Serial correlation coefficient is 0.000033 (totally uncorrelated = 0.0).

用于生成 1000000 个 key 的代码:

KeyGenerator des = KeyGenerator.getInstance("DES");
IntStream.range(0, 1_000_000).forEach(j -> {
SecretKey secretKey = des.generateKey();
System.out.write(secretKey.getEncoded());
});

熵较低,卡方分布表明分布不再是随机的。

所以我想知道Java的SecureRandom实现是否只是减少熵并直接从urandom 可能是更好的选择。

最佳答案

此处没有任何内容表明 SecureRandom 存在问题。

您将获得 DES key 的“每字节仅 7 位熵”结果,因为这就是 DES key 所具有的。 DES key 长 8 个字节,但 64 位中只有 56 位(即每字节 7 位)是随机的。每个字节中的第 8 位被保留用作该字节的奇偶校验位。奇偶校验位的值显然与其他 7 位的值高度相关,因此该位根本不是随机的。请参阅DES at Wikipedia了解更多背景。

如果您使用使用全随 secret 钥(例如“AES”)的算法的 key 生成器再次尝试测试,您应该会得到更令人欣慰的结果。

关于java - SecureRandom 是否会降低伪随机数据的熵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61042509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com