Java SecureRandom getInstance()方法及示例

Java SecureRandom getInstance()方法及示例

getInstance( String algorithm )

java.security.SecureRandom 类的 getInstance() 方法用于返回一个实现了指定的随机数发生器(RNG)算法的SecureRandom对象。

这个方法会遍历已注册的安全提供商的列表,从最喜欢的提供商开始。一个新的SecureRandom对象封装了来自第一个支持指定算法的提供者的SecureRandomSpi实现,并被返回。

语法

public static SecureRandom 
getInstance( String algorithm ) 
throws NoSuchAlgorithmException

参数: 该方法接受 标准RNG算法 作为参数。

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

异常: 该方法抛出 NoSuchAlgorithmException - 如果没有提供商支持指定算法的SecureRandomSpi实现。

注意

  1. 这些程序将不会在在线IDE上运行。
  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(String algorithm, Provider provider)

java.security.SecureRandom 类的 getInstance() 方法用于返回一个实现了指定的随机数发生器(RNG)算法的SecureRandom对象。
一个新的SecureRandom对象封装了来自指定的提供者对象的SecureRandomSpi实现,被返回。请注意,指定的提供者对象不需要在提供者列表中注册。

返回的SecureRandom对象还没有被播种。要对返回的对象进行播种,请调用setSeed方法。如果不调用setSeed,对nextBytes的第一次调用将强制SecureRandom对象进行自我播种。如果之前调用了setSeed,这种自我播种将不会发生。

语法

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

参数: 该方法接受以下参数作为参数

  • algorithm – RNG算法的名称。
  • provider – 提供者。

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

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

  • NoSuchAlgorithmException – 如果指定算法的SecureRandomSpi实现在指定的提供者对象中不可用。
  • IllegalArgumentException – 如果指定的提供者为空。

注意

  1. 这些程序将不会在在线IDE上运行。
  2. 每次安全随机类都会产生随机输出:

下面是说明getInstance()方法的例子。

例1:

// 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);
        }
    }
}

输出

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 failed

例2:

// 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);
        }
    }
}

输出

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

getInstance(String algorithm, String provider)

java.security.SecureRandom 类的 getInstance() 方法用于返回一个实现了指定的随机数发生器(RNG)算法的SecureRandom对象。

一个新的SecureRandom对象封装了来自指定提供者的SecureRandomSpi实现,并被返回。指定的提供者必须在安全提供者列表中注册。

语法

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

参数: 该方法接受以下参数作为参数

  • algorithm – RNG算法的名称。关于标准RNG算法名称的信息,请参见Java Cryptography Architecture Standard Algorithm Name Documentation中的SecureRandom部分。
  • provider – 提供者的名称。

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

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

  • NoSuchAlgorithmException – 如果指定算法的SecureRandomSpi实现无法从指定的提供者那里获得。
  • NoSuchProviderException – 如果指定的提供者没有在安全提供者列表中注册。
  • IllegalArgumentException – 如果提供者的名字是空的或空的。

注意。

  1. 这些程序将不会在在线IDE上运行。
  2. 每次安全随机类都会产生随机输出:

下面是说明getInstance()方法的例子。

例1:

// 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);
        }
    }
}

输出

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 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);
        }
    }
}

输出

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

例3:

// 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);
        }
    }
}

输出

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程