Java中的 KeyFactory getInstance() 方法及示例
getInstance(字符串算法)
Java.security.KeyFactory类的getInstance()方法返回一个 KeyFactory 类型的对象,该对象应用分配的 KeyFactory 算法。
句法:
public static KeyFactory
getInstance(String algorithm)
throws NoSuchAlgorithmException
参数:此方法寻求标准算法作为参数,其实例将被创建到此 KeyFactory 实例。
返回值:此方法返回一个新的密钥工厂对象。
异常:此方法抛出以下异常:
- NoSuchAlgorithmException:如果没有提供程序可用于支持特定算法的密钥工厂 spi 应用程序。
- 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 the object of KeyFactory
// and getting instance
// By using getInstance() method
KeyFactory sr
= KeyFactory.getInstance("DSA");
// getting the algorithm 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 using getInstance() method
KeyFactory sr = KeyFactory.getInstance("GEEKS");
// getting the algorithm 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(字符串算法,字符串提供者)
Java.security.KeyFactory类的getInstance()方法返回一个 KeyFactory 类型的对象,该对象应用分配的 KeyFactory 算法和分配的提供者对象。
句法:
public static KeyFactory
getInstance(String algorithm, String provider)
throws NoSuchAlgorithmException
参数:此方法寻求以下参数作为参数:
- algorithm :这是指定用于获取实例的算法的名称。
- provider :这是要指定的提供者的名称
返回值:此方法返回一个新的密钥工厂对象。
异常:此方法抛出以下异常:
- NoSuchAlgorithmException: – 如果没有提供程序可用于支持特定算法的密钥工厂 spi 应用程序。
- IllegalArgumentException: – 如果提供者为空。
- 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 the object of KeyFactory
// and getting instance
// By using getInstance() method
KeyFactory sr
= KeyFactory.getInstance(
"DSA", "SUN");
// getting the algorithm 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 using getInstance() method
KeyFactory sr
= KeyFactory.getInstance(
"RSA", "SUN");
// getting the algorithm 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
参考: https://docs.oracle.com/javase/9/docs/api/ Java/security/KeyFactory.html#getInstance-java.lang.String-