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
相關用法
- Java AlgorithmParameters getProvider()用法及代碼示例
- Java AlgorithmParameters toString()用法及代碼示例
- Java AlgorithmParameters getAlgorithm()用法及代碼示例
- Java KeyFactory getInstance()用法及代碼示例
- Java NumberFormat getInstance()用法及代碼示例
- Java DecimalFormatSymbols getInstance()用法及代碼示例
- Java KeyPairGenerator getInstance()用法及代碼示例
- Java Calendar getInstance()用法及代碼示例
- Java Currency getInstance()用法及代碼示例
- Java AlgorithmParameterGenerator getInstance()用法及代碼示例
- Java SecureRandom getInstance()用法及代碼示例
- Java Signature getInstance()用法及代碼示例
- Java MessageDigest getInstance()用法及代碼示例
- Java DecimalFormatSymbols getInstance(Locale)用法及代碼示例
- Java Collator getInstance()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 AlgorithmParameters getInstance() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。