當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。