SecureRandom是否会减少伪随机数据的熵?

问题描述 投票:0回答:1

我想知道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密钥,则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密钥的代码:

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可能是更好的选择。

java security random alpine des
1个回答
0
投票

这里什么都没有表示SecureRandom有问题。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.