zoukankan      html  css  js  c++  java
  • 【Java】API SecureRandom 安全随机

    之前学习的Random工具类只是一个伪随机数类。。。

    @Test
    public void secureRandom() throws Exception {
        //  个不可预测的安全的随机数
        // 无法指定种子,它使用RNG(random number generator)算法
        // 实际上有多种不同的底层实现,有的使用安全随机种子加上伪随机数算法来产生安
        //  SecureRandom常用工具类构建全的随机数,有的使用真正的随机数生成器。实际使用的时候,可以优先获取高强度的安全随机数生成器,如果没有提供,再使用普通等级的安全随机数生成器
    
        // 实例获取
        SecureRandom secureRandom = new SecureRandom();
    
        // jdk8 获取更安全的实例
        SecureRandom instanceStrong = SecureRandom.getInstanceStrong();
    
        String algorithm = secureRandom.getAlgorithm();
        Provider provider = secureRandom.getProvider();
        String providerInfo = provider.getInfo();
    
        System.out.println("normalInstance algorithm -> " + algorithm + "
    provider -> " + providerInfo);
    
        System.out.println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
    
        algorithm = instanceStrong.getAlgorithm();
        provider = instanceStrong.getProvider();
        providerInfo = provider.getInfo();
    
        System.out.println("strongInstance algorithm -> " + algorithm + "
    provider -> " + providerInfo);
    }

    打印结果:

    normalInstance algorithm -> SHA1PRNG
    provider -> SUN (DSA key/parameter generation; DSA signing; SHA-1, MD5 digests; SecureRandom; X.509 certificates; JKS & DKS keystores; PKIX CertPathValidator; PKIX CertPathBuilder; LDAP, Collection CertStores, JavaPolicy Policy; JavaLoginConfig Configuration)
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    strongInstance algorithm -> Windows-PRNG
    provider -> Sun's Microsoft Crypto API provider
    
    Process finished with exit code 0

    获取随机数实例和Random类实例是一样的。

  • 相关阅读:
    Welcome to my website
    花生壳
    Gentle.NET Attribute
    发布WebFtp 控件(ASP.NET控件,用以web方式进行文件上下传操作)
    发布数据库连接字符串生成工具
    发布语法加亮控件(SyntaxTextBox)
    .NET中现有的 ORM 工具(转)
    发布Oracle存储过程包c#代码生成工具(CodeRobot)
    ASCII码表
    爱上语法高亮控件ICSharpCode.TextEditor ~o~
  • 原文地址:https://www.cnblogs.com/mindzone/p/13762806.html
Copyright © 2011-2022 走看看