热门标签 | 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



推荐阅读
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 使用GDI的一些AIP函数我们可以轻易的绘制出简 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • ImmutableX Poised to Pioneer Web3 Gaming Revolution
    ImmutableX is set to spearhead the evolution of Web3 gaming, with its innovative technologies and strategic partnerships driving significant advancements in the industry. ... [详细]
  • 本文详细介绍了Java中的访问器(getter)和修改器(setter),探讨了它们在保护数据完整性、增强代码可维护性方面的重要作用。通过具体示例,展示了如何正确使用这些方法来控制类属性的访问和更新。 ... [详细]
  • andr ... [详细]
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
  • 基于KVM的SRIOV直通配置及性能测试
    SRIOV介绍、VF直通配置,以及包转发率性能测试小慢哥的原创文章,欢迎转载目录?1.SRIOV介绍?2.环境说明?3.开启SRIOV?4.生成VF?5.VF ... [详细]
  • 解决JAX-WS动态客户端工厂弃用问题并迁移到XFire
    在处理Java项目中的JAR包冲突时,我们遇到了JaxWsDynamicClientFactory被弃用的问题,并成功将其迁移到org.codehaus.xfire.client。本文详细介绍了这一过程及解决方案。 ... [详细]
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社区 版权所有