Java 中的 KeyFactory getAlgorithm() 方法

java 8programmingobject oriented programming

可以使用 java.security.KeyFactory 类中的 getAlgorithm() 方法获取 KeyFactory 的算法名称。此方法无需参数,并返回 KeyFactory 的算法名称。

以下程序演示了此操作 −

示例

import java.security.*;
import java.util.*;
import java.security.spec.*;
public class Demo {
   public static void main(String[] argv) throws Exception {
      try {
         KeyFactory kf = KeyFactory.getInstance("RSA");
         String algorithm = kf.getAlgorithm();
         System.out.println("The Algortihm is: " + algorithm);
      } catch (NoSuchAlgorithmException e) {
         System.out.println("Error!!! NoSuchAlgorithmException");
      } catch (ProviderException e) {
         System.out.println("Error!!! ProviderException");
      }
   }
}

输出

The Algorithm is: RSA

现在让我们理解一下上面的程序。

getAlgorithm() 方法用于获取 KeyFactory 的算法名称。然后显示该算法名称。如果算法名称错误,则会抛出 NoSuchAlgorithmException 异常。以下是演示代码片段。 −

try {
   KeyFactory kf = KeyFactory.getInstance("RSA");
   String algorithm = kf.getAlgorithm();
   System.out.println("The Algortihm is: " + algorithm);
} catch (NoSuchAlgorithmException e) {
   System.out.println("Error!!! NoSuchAlgorithmException");
} catch (ProviderException e) {
   System.out.println("Error!!! ProviderException");
}

相关文章