【问题标题】:Does SecureRandom reduce entropy of pseudo-random data?SecureRandom 会减少伪随机数据的熵吗?
【发布时间】:2020-04-05 12:22:51
【问题描述】:

我想知道 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());
});

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

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

【问题讨论】:

    标签: java security random alpine des


    【解决方案1】:

    这里没有任何迹象表明SecureRandom 有问题。

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

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

    【讨论】:

    • 好点!奇偶校验位也可以解释卡方分布的退化吗?
    • 是的,当然。奇校验位意味着一半可能的 8 位值永远不会出现在数据中(b'10000000' 但永远不会出现 b'00000000'、b'00000001' 但永远不会出现 b'10000001' 等等)和值确实出现的情况将是真正随机样本中预期的两倍。那些空的和双重填充的样本桶的累积卡方分数将是巨大的。理想情况下(即,如果字节的低 7 位是真正随机的)它将等于样本数。这和你得到的结果一致,在样本数的 0.003% 以内。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 2014-11-07
    相关资源
    最近更新 更多