java - RHEL 7 不尊重 java 安全随机种子

标签 java rhel rhel7 secure-random

我手上有一个难题。我创建了一个 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/

相关文章:

java - 如何防止SIGINT信号杀死Java中的子进程

Java Web Start 无法启动

java - 通过jython访问Python中的静态java方法

msbuild - 防止 .NET Core 2.0 在 RHEL7 上将文件保留在/tmp 中

rhel - 显式声明 lib 时出现 UnsatisfiedLinkError 异常

amazon-web-services - yum 从错误的存储库更新 i

linux - 在 grub2.cfg 中修改内核引导选项的正确方法是什么?

linux - gitlab 管理区域出现 500 错误

Java代码、ASCII艺术、switch语句/结构、输入字符然后将其打印在屏幕上

docker-compose - systemd:似乎 ExecStop 脚本在启动命令运行后立即执行