我手上有一个难题。我创建了一个 AES 服务来加密/解密敏感信息。 AES key 是使用 java 的 SecureRandom
随机生成的。我有一个存储种子的 protected 文件,在调用该服务时,种子被填充到 Secure Random 类中。
为了确保它有效,我有以下逻辑:
private boolean secureRandom(final String seed) {
SecureRandom sr1 = new SecureRandom(seed.getBytes(UTF8_CHARSET));
SecureRandom sr2 = new SecureRandom(seed.getBytes(UTF8_CHARSET));
//Two secure random with the same seed should generate the same results
boolean secureRandomWorks = sr1.nextLong() == sr2.nextLong();
if (!secureRandomWorks) {
System.err.println("Secure random not supported. Defaulting to old key");
}
return secureRandomWorks;
}
这里的想法是我应该能够创建两个具有相同种子的安全随机对象,并且它们应该在调用 nextLong()
当我在 Windows 机器上部署我的应用程序时,一切正常,但当我将它部署在 RHEL 7 机器上时,出现错误。
我的印象是只要种子相同,两个实例将始终产生相同的输出。在 Windows 上似乎是这种情况,但当我在 RHEL 7 上测试时似乎并非如此。
我创建了这个简单的测试来验证:
SecureRandom sr1 = new SecureRandom("encryptionKey".getBytes("UTF-8"));
SecureRandom sr2 = new SecureRandom("encryptionKey".getBytes("UTF-8"));
for (int i = 0; i < 1000; i++) {
System.out.println(sr1.nextLong() == sr2.nextLong());
}
在 Windows 上,每个输出都是 true,而在 RHEL 7 上,这是 false。
关于可能导致 RHEL 7 忽略种子的任何想法的建议?
最佳答案
我没有找到任何文件禁止您在 RHEL 7 上观察到的行为。
java.util.Random
的 JavaDoc明确指出
If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers
java.security.SecureRandom
的 JavaDoc不包含类似的声明。
相反,它提到(在 setSeed()
方法的文档中)
Reseeds this random object. The given seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.
关于java - RHEL 7 不尊重 java 安全随机种子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50107982/