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


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


getInstance(String algorithm)

java.security.AlgorithmParameters類的getInstance()方法返回應用分配的AlgorithmParameters算法的AlgorithmParameters類型的對象。

用法:

public static AlgorithmParameters
  getInstance(String algorithm)
  throws NoSuchAlgorithmException

參數:此方法將標準算法作為參數。


返回值:該方法提供了一個新的算法參數對象。

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

  • NoSuchAlgorithmException:–如果沒有提供者可支持特定算法的算法參數spi應用程序。
  • 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 { 
            // creating the object of AlgorithmParameters 
            // and getting instance 
            // By usng getInstance() method 
            AlgorithmParameters sr 
                = AlgorithmParameters 
                      .getInstance("DES"); 
  
            // getting the status 
            // of AlgorithmParameters 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 : null

示例2:顯示NoSuchAlgorithmException

// 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 AlgorithmParameters 
            // and getting instance 
            // By usng getInstance() method 
            AlgorithmParameters sr 
                = AlgorithmParameters.getInstance("GFG"); 
  
            // getting the status 
            // of AlgorithmParameters 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 AlgorithmParameters not available

getInstance(String algorithm, Provider provider)

java.security.AlgorithmParameters類的getInstance()方法返回一個AlgorithmParameters類型的對象,該對象應用分配的algorithmParameters算法和分配的提供者對象。

用法:

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

參數:此方法尋求以下參數作為參數:


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

返回值:該方法提供了一個新的算法參數對象。

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

  • NoSuchAlgorithmException:如果沒有提供者可支持特定算法的算法參數spi應用程序。
  • 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 { 
            // creating AlgorithmParameterGenerator object 
            // and getting instance 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr 
                = AlgorithmParameterGenerator 
                      .getInstance("DiffieHellman"); 
  
            // creating Provider object 
            Provider pd = sr.getProvider(); 
  
            // getting algorithm name 
            // by using getAlgorithm() mathod 
            String algo = sr.getAlgorithm(); 
  
            // creating AlgorithmParameterGenerator object 
            // and getting instance 
            // 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@63947c6b

示例2:顯示NoSuchAlgorithmException

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // creating AlgorithmParameterGenerator object 
            // and getting instance 
            // 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(); 
  
            // creating AlgorithmParameterGenerator object 
            // and getting instance 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr1 
                = AlgorithmParameterGenerator 
                      .getInstance("GFG", 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:
 no such algorithm: GFG for provider SUN

參考: https://docs.oracle.com/javase/9/docs/api/java/security/AlgorithmParameters.html



相關用法


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