当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java SecureRandom getInstance()用法及代码示例


getInstance( String algorithm )

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

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

用法:


public static SecureRandom 
getInstance( String algorithm ) 
throws NoSuchAlgorithmException

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

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

异常:如果没有提供者支持指定算法的SecureRandomSpi实现,则此方法引发NoSuchAlgorithmException。

注意:

  1. 该程序将无法在在线IDE上运行。
  2. 每次Secure Random类将生成随机输出。

下面是说明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 usng 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 usng 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对象。
返回一个封装了指定Provider对象中的SecureRandomSpi实现的新SecureRandom对象。请注意,指定的提供程序对象不必在提供程序列表中注册。

返回的SecureRandom对象尚未设定种子。要播种返回的对象,请调用setSeed方法。如果未调用setSeed,则对nextBytes的第一次调用将强制SecureRandom对象播种自身。如果先前调用过setSeed,则不会发生此self-seeding。

用法:

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

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

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

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

异常:此方法引发以下异常

  • NoSuchAlgorithmException –如果指定的Provider对象不提供指定算法的SecureRandomSpi实现。
  • IllegalArgumentException –如果指定的提供者为null。
  • 注意:

  1. 该程序将无法在在线IDE上运行。
  2. 每次Secure Random类将生成随机输出。

下面是说明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 usng 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:初始化失败

示例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 usng 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对象。

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

用法:

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

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

  • algorithm – RNG算法的名称。有关标准RNG算法名称的信息,请参见Java密码体系结构标准算法名称文档中的SecureRandom部分。
  • provider –提供者的名称。

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

异常:此方法引发以下异常


  • NoSuchAlgorithmException–如果指定提供者无法使用指定算法的SecureRandomSpi实现。
  • NoSuchProviderException–如果指定的提供者未在安全提供者列表中注册。
  • IllegalArgumentException–如果提供者名称为null或为空。

注意:

  1. 该程序将无法在在线IDE上运行。
  2. 每次Secure Random类将生成随机输出。

下面是说明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 usng 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 usng 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 usng 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


相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 SecureRandom getInstance() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。