来自 java.util.Random
的 java.security.SecureRandom
种子是否会削弱加密强随机数生成器?
我看到这段代码,想知道为什么要以这种特定的方式完成。
randomGenerator = new SecureRandom();
final Random rnd = new Random();
randomGenerator.setSeed(rnd.nextLong());
从文档来看,调用setSeed
永远不会减少随机性。那么为什么还要调用 setSeed
呢?
public void setSeed(long seed)
Reseeds this random object, using the eight bytes contained in the given long seed. The given seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.
docs.oracle.com
最佳答案
使用 CSPRNG 时,加密强度与用于种子的熵的位强度相同。
CSPRNG 将产生几乎无限量的伪随机熵,尽管实际上该伪随机熵的实际强度仅与 CSPRNG 底层种子状态的熵一样强。
因此,如果您仅使用来自java.util.Random
的种子,您就会遇到麻烦。
幸运的是,内核和其他源用于为 java.security.SecureRandom
使用的系统 CSPRNG 提供种子,因此“添加”/混合到其中,如上面的示例代码所示,可以不要让它变得“不那么”随机或减少熵。
所以:
- 是的,
java.util.Random
种子毫无值(value)。 - 这没关系,因为系统已经从硬件中断、启动抖动和其他噪声中播种了 CSPRNG。
- 无论谁写了这个:
randomGenerator.setSeed(rnd.nextLong());
都不应该涉及加密工程。
关于java - 当使用随机种子时,SecureRandom 是否会减弱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67722610/