getInstance(String algorithm)
java.security.KeyFactory類的getInstance()方法返回一個KeyFactory類型的對象,該對象應用分配的KeyFactory算法。
用法:
public static KeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
參數:此方法將標準算法視為要為其實例創建此KeyFactory實例的參數。
返回值:此方法返回一個新的 key 工廠對象。
異常:此方法引發以下異常:
- NoSuchAlgorithmException:如果沒有提供者可用於支持特定算法的關鍵工廠spi應用程序。
- NullPointerException :如果algorithm為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 KeyFactory
// and getting instance
// By usng getInstance() method
KeyFactory sr
= KeyFactory.getInstance("DSA");
// getting the algoritm of KeyFactory object
String str = sr.getAlgorithm();
// printing the status
System.out.println("algorithm : " + str);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
}
}
輸出:
algorithm : DSA
示例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 KeyFactory
// and getting instance
// By usng getInstance() method
KeyFactory sr = KeyFactory.getInstance("GEEKS");
// getting the algoritm of KeyFactory object
String str = sr.getAlgorithm();
// printing the status
System.out.println("algorithm : " + str);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
}
}
輸出:
Exception thrown : java.security.NoSuchAlgorithmException: GEEKS KeyFactory not available
getInstance(String algorithm, String provider)
java.security.KeyFactory類的getInstance()方法返回一個KeyFactory類型的對象,該對象應用分配的KeyFactory算法和分配的提供程序對象。
用法:
public static KeyFactory getInstance(String algorithm, String provider) throws NoSuchAlgorithmException
參數:此方法尋求以下參數作為參數:
- algorithm:這是為獲取實例而指定的算法的名稱。
- provider:這是要指定的提供者的名稱
返回值:此方法返回一個新的 key 工廠對象。
異常:此方法引發以下異常:
- NoSuchAlgorithmException:–如果沒有提供者可用於支持特定算法的關鍵工廠spi應用程序。
- IllegalArgumentException:–如果提供者為null。
- 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 KeyFactory
// and getting instance
// By usng getInstance() method
KeyFactory sr
= KeyFactory.getInstance(
"DSA", "SUN");
// getting the algoritm of KeyFactory object
String str = sr.getAlgorithm();
// printing the status
System.out.println("algorithm : " + str);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
catch (NoSuchProviderException e) {
System.out.println("Exception thrown : " + e);
}
}
}
輸出:
algorithm : DSA
示例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 KeyFactory
// and getting instance
// By usng getInstance() method
KeyFactory sr
= KeyFactory.getInstance(
"RSA", "SUN");
// getting the algoritm of KeyFactory object
String str = sr.getAlgorithm();
// printing the status
System.out.println("algorithm : " + str);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
catch (NoSuchProviderException e) {
System.out.println("Exception thrown : " + e);
}
}
}
輸出:
Exception thrown : java.security.NoSuchAlgorithmException: no such algorithm: RSA for provider SUN
相關用法
- Java KeyFactory getProvider()用法及代碼示例
- Java KeyFactory generatePublic()用法及代碼示例
- Java KeyFactory getAlgorithm()用法及代碼示例
- Java KeyFactory generatePrivate()用法及代碼示例
- Java NumberFormat getInstance()用法及代碼示例
- Java Currency getInstance()用法及代碼示例
- Java MessageDigest getInstance()用法及代碼示例
- Java KeyPairGenerator getInstance()用法及代碼示例
- Java DecimalFormatSymbols getInstance()用法及代碼示例
- Java AlgorithmParameters getInstance()用法及代碼示例
- Java AlgorithmParameterGenerator getInstance()用法及代碼示例
- Java Signature getInstance()用法及代碼示例
- Java Calendar getInstance()用法及代碼示例
- Java SecureRandom getInstance()用法及代碼示例
- Java DecimalFormatSymbols getInstance(Locale)用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 KeyFactory getInstance() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。