热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Java中的SecureRandomgetInstance()方法,带示例

Java中的SecureRandomgetInstance()方法,带示例

Java 中的 SecureRandom getInstance()方法,带示例

原文:https://www . geesforgeks . org/secureandom-getinstance-method-in-Java-with-examples/


字符串算法

Java . security . SecureRandom类的 getInstance() 方法用于返回实现指定随机数生成器(RNG)算法的 secureandom 对象。

此方法遍历注册的安全提供程序列表,从最首选的提供程序开始。返回一个新的安全对象,该对象封装了来自第一个支持指定算法的提供程序的安全对象。

语法:

public static SecureRandom
getInstance( String algorithm )
throws NoSuchAlgorithmException

参数:该方法以标准 RNG 算法为参数。

返回值:这个方法返回新的 SecureRandom 对象。

异常:如果没有提供程序支持指定算法的 SecureRandomSpi 实现,此方法将引发nosuchalgorithm 异常

注:


  1. 这些程序不会在联机集成开发环境中运行。

  2. 每次安全随机类都会产生随机输出。

以下是说明 getInstance() 方法的示例:

例 1:

// Java program to demonstrate
// nextBytes() method
import java.security.*;
import java.util.*;
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating the object of SecureRandom and getting instance
            // By using getInstance() method
            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
            // Declaring the string variable
            String str = "Tajmahal";
            // Declaring the byte Array
            // converting string into byte
            byte[] b = str.getBytes();
            // printing the byte array
            System.out.println("Byte array before operation : "
                               + Arrays.toString(b));
            // generating user-specified number of random bytes
            // using nextBytes() method
            sr.nextBytes(b);
            // printing the new byte array
            System.out.println("Byte array after operation : "
                               + Arrays.toString(b));
        }
        catch (NoSuchAlgorithmException e) {
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

输出:

Byte array before operation : [84, 97, 106, 109, 97, 104, 97, 108]
Byte array after operation : [124, -66, -62, -5, -71, -4, 30, 16]

例 2:

// Java program to demonstrate
// getInstance() method
import java.security.*;
import java.util.*;
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating the object of SecureRandom and getting instance
            // By using getInstance() method
            System.out.println("Trying to get the instance of TAJMAHAL");
            SecureRandom sr = SecureRandom.getInstance("TAJMAHAL");
            // Declaring the string variable
            String str = "Tajmahal";
            // Declaring the byte Array
            // converting string into byte
            byte[] b = str.getBytes();
            // printing the byte array
            System.out.println("Byte array before operation : "
                               + Arrays.toString(b));
            // generating user-specified number of random bytes
            // using nextBytes() method
            sr.nextBytes(b);
            // printing the new byte array
            System.out.println("Byte array after operation : "
                               + Arrays.toString(b));
        }
        catch (NoSuchAlgorithmException e) {
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

输出:

Trying to get the instance of TAJMAHAL
Exception thrown : java.security.NoSuchAlgorithmException: TAJMAHAL SecureRandom not available


getInstance(字符串算法,提供程序提供程序)

Java . security . SecureRandom类的 getInstance() 方法用于返回实现指定随机数生成器(RNG)算法的 secureandom 对象。
返回一个新的安全对象,该对象封装了来自指定提供程序对象的安全对象接口实现。请注意,指定的提供程序对象不必在提供程序列表中注册。

返回的安全对象尚未植入。若要为返回的对象设定种子,请调用 setSeed 方法。如果不调用 setSeed,对 nextBytes 的第一次调用将强制 SecureRandom 对象对自身进行种子化。如果之前调用了 setSeed,则不会发生这种自播种。

语法:

public static SecureRandom
getInstance( String algorithm, Provider provider )
throws NoSuchAlgorithmException

参数:该方法将以下参数作为参数


  • 算法–RNG 算法的名称。

  • 提供商–提供商。

返回值:该方法返回新的 SecureRandom 对象

异常:该方法抛出以下异常


  • no suchalgorithm exception–如果指定的提供程序对象中没有指定算法的 SecureRandomSpi 实现,则为。

  • IllegalArgumentException – if the specified provider is null.

    注:


    1. 这些程序不会在联机集成开发环境中运行。

    2. 每次安全随机类都会产生随机输出。

    以下是说明 getInstance() 方法的示例:

    例 1:

    ```java
    // Java program to demonstrate
    // getInstance() method

    import java.security.;
    import java.util.
    ;

    public class GFG1 {
        public static void main(String[] argv)
        {
            try {
                // creating SecureRandom object
                SecureRandom sr1 = new SecureRandom(new byte[] { 1, 2, 3, 4 });

    // creating Provider object
                Provider pd = sr1.getProvider();

    // creating the object of SecureRandom and getting instance
                // By using getInstance() method

    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", pd);

    // Declaring the string variable
                String str = "Tajmahal";

    // Declaring the byte Array
                // converting string into byte
                byte[] b = str.getBytes();

    // printing the byte array
                System.out.println("Byte array before operation : "
                                   + Arrays.toString(b));

    // generating user-specified number of random bytes
                // using nextBytes() method
                sr.nextBytes(b);

    // printing the new byte array
                System.out.println("Byte array after operation : "
                                   + Arrays.toString(b));
            }

    catch (NoSuchAlgorithmException e) {

    System.out.println("Exception thrown : " + e);
            }
            catch (ProviderException e) {

    System.out.println("Exception thrown : " + e);
            }
        }
    }
    ```

    输出:

    java
    Byte array before operation : [84, 97, 106, 109, 97, 104, 97, 108]
    Byte array after operation : [109, 55, 116, -15, -83, 126, -128, 88]

    注意:以下程序在联机 IDE 中产生以下异常
    抛出异常:Java . security . providerexception:init 失败

    例 2:

    ```java
    // Java program to demonstrate
    // getInstance() method

    import java.security.;
    import java.util.
    ;

    public class GFG1 {
        public static void main(String[] argv)
        {
            try {
                // creating SecureRandom object
                SecureRandom sr1 = new SecureRandom(new byte[] { 1, 2, 3, 4 });

    // creating Provider object
                Provider pd = sr1.getProvider();

    // creating the object of SecureRandom and getting instance
                // By using getInstance() method
                System.out.println("Trying to getting the instance of TAJMAHAL ");
                SecureRandom sr = SecureRandom.getInstance("TAJMAHAL", pd);

    // Declaring the string variable
                String str = "Tajmahal";

    // Declaring the byte Array
                // converting string into byte
                byte[] b = str.getBytes();

    // printing the byte array
                System.out.println("Byte array before operation : "
                                   + Arrays.toString(b));

    // generating user-specified number of random bytes
                // using nextBytes() method
                sr.nextBytes(b);

    // printing the new byte array
                System.out.println("Byte array after operation : "
                                   + Arrays.toString(b));
            }

    catch (NoSuchAlgorithmException e) {

    System.out.println("Exception thrown : " + e);
            }
            catch (ProviderException e) {

    System.out.println("Exception thrown : " + e);
            }
        }
    }
    ```

    输出:

    java
    Trying to getting the instance of TAJMAHAL
    Exception thrown : java.security.NoSuchAlgorithmException: no such algorithm: TAJMAHAL for provider SUN

    getInstance(字符串算法,字符串提供程序)

    Java . security . SecureRandom类的 getInstance() 方法用于返回实现指定随机数生成器(RNG)算法的 secureandom 对象。

    返回一个新的安全对象,该对象封装了来自指定提供程序的安全对象接口实现。指定的提供程序必须在安全提供程序列表中注册。

    语法:

    java
    public static SecureRandom
    getInstance( String algorithm, String provider )
    throws NoSuchAlgorithmException, NoSuchProviderException

    参数:该方法将以下参数作为参数


    • 算法–RNG 算法的名称。有关标准 RNG 算法名称的信息,请参见 Java 加密体系结构标准算法名称文档中的安全域部分。

    • 提供者–提供者的名称。

    返回值:该方法返回新的 SecureRandom 对象

    异常:该方法抛出以下异常


    • no suchalgorithm exception–如果指定的提供程序无法提供指定算法的 SecureRandomSpi 实现。

    • NoSuchProviderException–如果指定的提供商未在安全提供商列表中注册。

    • IllegalArgumentException–如果提供程序名称为空。

    注:


    1. 这些程序不会在联机集成开发环境中运行。

    2. 每次安全随机类都会产生随机输出。

    以下是说明 getInstance() 方法的示例:

    例 1:

    ```java
    // Java program to demonstrate
    // getInstance() method

    import java.security.;
    import java.util.
    ;

    public class GFG1 {
        public static void
            main(String[] argv)
                throws NoSuchAlgorithmException,
                       NoSuchProviderException
        {
            try {
                // creating SecureRandom object
                SecureRandom sr1 = new SecureRandom(new byte[] { 1, 2, 3, 4 });

    // creating Provider object
                Provider pd = sr1.getProvider();

    // getting provider name
                // by using method getname()
                String provider = pd.getName();

    // getting algorithm name
                // by using     getAlgorithm() mathod
                String algo = sr1.getAlgorithm();

    // creating the object of SecureRandom and getting instance
                // By using getInstance() method
                SecureRandom sr = SecureRandom.getInstance(algo, provider);

    // Declaring the string variable
                String str = "Tajmahal";

    // Declaring the byte Array
                // converting string into byte
                byte[] b = str.getBytes();

    // printing the byte array
                System.out.println("Byte array before operation : "
                                   + Arrays.toString(b));

    // generating user-specified number of random bytes
                // using nextBytes() method
                sr.nextBytes(b);

    // printing the new byte array
                System.out.println("Byte array after operation : "
                                   + Arrays.toString(b));
            }

    catch (NoSuchAlgorithmException e) {

    System.out.println("Exception thrown : " + e);
            }
            catch (ProviderException e) {

    System.out.println("Exception thrown : " + e);
            }
        }
    }
    ```

    输出:

    java
    Byte array before operation : [84, 97, 106, 109, 97, 104, 97, 108]
    Byte array after operation : [-11, 81, 39, 67, -95, -51, 115, -18]

    例 2:

    ```java
    // Java program to demonstrate
    // getInstance() method

    import java.security.;
    import java.util.
    ;

    public class GFG1 {
        public static void
            main(String[] argv)
                throws NoSuchAlgorithmException,
                       NoSuchProviderException
        {
            try {
                // creating SecureRandom object
                SecureRandom sr1 = new SecureRandom(new byte[] { 1, 2, 3, 4 });

    // creating Provider object
                Provider pd = sr1.getProvider();

    // getting provider name
                // by using method getname()
                String provider = pd.getName();

    // creating the object of SecureRandom and getting instance
                // By using getInstance() method
                System.out.println("Trying to take TAJMAHAL as a algorithm");
                SecureRandom sr = SecureRandom.getInstance("TAJMAHAL", provider);

    // Declaring the string variable
                String str = "Tajmahal";

    // Declaring the byte Array
                // converting string into byte
                byte[] b = str.getBytes();

    // printing the byte array
                System.out.println("Byte array before operation : "
                                   + Arrays.toString(b));

    // generating user-specified number of random bytes
                // using nextBytes() method
                sr.nextBytes(b);

    // printing the new byte array
                System.out.println("Byte array after operation : "
                                   + Arrays.toString(b));
            }

    catch (NoSuchAlgorithmException e) {

    System.out.println("Exception thrown : " + e);
            }
            catch (ProviderException e) {

    System.out.println("Exception thrown : " + e);
            }
        }
    }
    ```

    输出:

    java
    Trying to take TAJMAHAL as a algorithm
    Exception thrown : java.security.NoSuchAlgorithmException: no such algorithm: TAJMAHAL for provider SUN

    例 3:

    ```java
    // Java program to demonstrate
    // getInstance() method

    import java.security.;
    import java.util.
    ;

    public class GFG1 {
        public static void
            main(String[] argv)
                throws NoSuchAlgorithmException,
                       NoSuchProviderException
        {
            try {
                // creating SecureRandom object
                SecureRandom sr1 = new SecureRandom(new byte[] { 1, 2, 3, 4 });

    // getting algorithm name
                // by using     getAlgorithm() mathod
                String algo = sr1.getAlgorithm();

    // creating the object of SecureRandom and getting instance
                // By using getInstance() method
                System.out.println("Trying to taking MOON as a provider");
                SecureRandom sr = SecureRandom.getInstance(algo, "MOON");

    // Declaring the string variable
                String str = "Tajmahal";

    // Declaring the byte Array
                // converting string into byte
                byte[] b = str.getBytes();

    // printing the byte array
                System.out.println("Byte array before operation : "
                                   + Arrays.toString(b));

    // generating user-specified number of random bytes
                // using nextBytes() method
                sr.nextBytes(b);

    // printing the new byte array
                System.out.println("Byte array after operation : "
                                   + Arrays.toString(b));
            }

    catch (NoSuchAlgorithmException e) {

    System.out.println("Exception thrown : " + e);
            }
            catch (NoSuchProviderException e) {

    System.out.println("Exception thrown : " + e);
            }
        }
    }
    ```

    输出:

    java
    Trying to taking MOON as a provider
    Exception thrown : java.security.NoSuchProviderException: no such provider: MOON



推荐阅读
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • Java 中 Writer flush()方法,示例 ... [详细]
  • 本文将介绍如何使用 Go 语言编写和运行一个简单的“Hello, World!”程序。内容涵盖开发环境配置、代码结构解析及执行步骤。 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 本文介绍如何使用阿里云的fastjson库解析包含时间戳、IP地址和参数等信息的JSON格式文本,并进行数据处理和保存。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • C++实现经典排序算法
    本文详细介绍了七种经典的排序算法及其性能分析。每种算法的平均、最坏和最好情况的时间复杂度、辅助空间需求以及稳定性都被列出,帮助读者全面了解这些排序方法的特点。 ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
author-avatar
招聘HR
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有