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


Java AlgorithmParameterGenerator getInstance()用法及代碼示例


getInstance(String algorithm)

java.security.AlgorithmParameterGenerator類的getInstance()方法用於返回實現指定算法的AlgorithmParameterGenerator類型的對象。

用法:

public static AlgorithmParameterGenerator 
  getInstance(String algorithm)
    throws NoSuchAlgorithmException

參數:此方法將“算法”的標準名稱作為參數。


返回值:此方法返回新的AlgorithmParameterGenerator對象。

異常:此方法引發以下異常:

  • NoSuchAlgorithmException:如果沒有提供者支持指定算法的AlgorithmParameterGeneratorSpi實現。
  • NullPointerException :如果指定的算法為null。

下麵是說明getInstance()方法的示例:

示例1:

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // Getting instance of 
            // AlgorithmParameterGenerator 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr 
                = AlgorithmParameterGenerator 
                      .getInstance("DSA"); 
  
            // getting the status of 
            // AlgorithmParameterGenerator object 
            String str = sr.toString(); 
  
            // printing the status 
            System.out.println("Status: "
                               + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
    } 
}
輸出:
Status: java.security.AlgorithmParameterGenerator@232204a1

示例2:顯示NoSuchAlgorithmException

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // Getting instance of 
            // AlgorithmParameterGenerator 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr 
                = AlgorithmParameterGenerator 
                      .getInstance("GFG"); 
  
            // getting the status of 
            // AlgorithmParameterGenerator object 
            String str = sr.toString(); 
  
            // printing the status 
            System.out.println("Status: " + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
    } 
}
輸出:
Exception thrown:
 java.security.NoSuchAlgorithmException:
 GFG AlgorithmParameterGenerator not available

Signature getInstance(String algorithm, Provider provider)

java.security.AlgorithmParameterGenerator類的getInstance()方法用於返回實現指定簽名算法和指定提供者對象的AlgorithmParameterGenerator對象。

用法:

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

參數:此方法將以下參數作為參數:


  • algorithm:這是所請求算法的名稱。
  • provider:指定的提供者

返回值:此方法返回新的AlgorithmParameterGenerator對象。

異常:此方法引發以下異常:

  • NoSuchAlgorithmException:如果指定的Provider對象不提供指定算法的AlgorithmParameterGeneratorSpi實現。
  • IllegalArgumentException:如果提供者為null。
  • NullPointerException :如果算法為空

下麵是說明getInstance()方法的示例:

示例1:

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // Getting instance of 
            // AlgorithmParameterGenerator 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr 
                = AlgorithmParameterGenerator 
                      .getInstance("DSA"); 
  
            // creating Provider object 
            Provider pd = sr.getProvider(); 
  
            // getting algorithm name 
            // by using getAlgorithm() mathod 
            String algo = sr.getAlgorithm(); 
  
            // Getting instance of 
            // AlgorithmParameterGenerator 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr1 
                = AlgorithmParameterGenerator 
                      .getInstance(algo, pd); 
  
            // getting the status of 
            // AlgorithmParameterGenerator object 
            String str = sr1.toString(); 
  
            // printing the status 
            System.out.println("Status: "
                               + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
        catch (IllegalArgumentException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
    } 
}
輸出:
Status: java.security.AlgorithmParameterGenerator@232204a1

示例2:顯示NoSuchAlgorithmException

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // Getting instance of 
            // AlgorithmParameterGenerator 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr 
                = AlgorithmParameterGenerator 
                      .getInstance("GFG"); 
  
            // creating Provider object 
            Provider pd = sr.getProvider(); 
  
            // getting algorithm name 
            // by using getAlgorithm() mathod 
            String algo = sr.getAlgorithm(); 
  
            // Getting instance of 
            // AlgorithmParameterGenerator 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr1 
                = AlgorithmParameterGenerator 
                      .getInstance(algo, pd); 
  
            // getting the status of 
            // AlgorithmParameterGenerator object 
            String str = sr1.toString(); 
  
            // printing the status 
            System.out.println("Status: " + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
        catch (IllegalArgumentException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
    } 
}
輸出:
Exception thrown:
 java.security.NoSuchAlgorithmException:
 GFG AlgorithmParameterGenerator not available

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 AlgorithmParameterGenerator getInstance() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。