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

时间:2020-04-05 12:22:51

标签: java security random alpine des

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

1 个答案:

答案 0 :(得分:1)

这里没有任何内容表明SecureRandom有问题。

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

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

相关问题