【问题标题】:Why does the following SecureRandom NativePRNG fail on windows?为什么以下 SecureRandom NativePRNG 在 Windows 上失败?
【发布时间】:2023-02-15 08:34:27
【问题描述】:
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class Main {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        SecureRandom srand = SecureRandom.getInstance("NativePRNG");
        System.out.println(srand.nextInt());
    }
}

如何在 Windows 上使用 NativePRNG 运行?

【问题讨论】:

    标签: java os-dependent


    【解决方案1】:

    代码在 Windows 上失败的原因是因为“NativePRNG”算法并非在所有平台上都可用,包括 Windows。这是因为“NativePRNG”依赖于特定于平台的随机源,并且实现可能因不同的操作系统而异。

    要在 Windows 上运行“NativePRNG”,您可以安装 Java 加密扩展 (JCE) 无限强度管辖策略文件,其中包括适用于 Windows 的“NativePRNG”实现。安装 JCE Unlimited Strength Policy Files 后,您可以修改代码以明确指定“NativePRNG”算法提供程序:

    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    
    public class Main {
        public static void main(String[] args) throws NoSuchAlgorithmException {
            SecureRandom srand = SecureRandom.getInstance("NativePRNG", "SUN");
            System.out.println(srand.nextInt());
        }
    }
    

    请注意,您应该将“SUN”替换为在您的特定平台上支持“NativePRNG”算法的提供商的名称,因为不同的提供商可能在不同的平台上支持不同的算法。

    【讨论】:

      猜你喜欢
      • 2015-02-21
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 2013-08-23
      • 2018-04-25
      • 2023-04-01
      • 2011-03-04
      • 2017-05-03
      相关资源
      最近更新 更多