KeyFactory getAlgorithm() 方法在 Java 中


算法名称可使用 java.security.KeyFactory 中的方法 getAlgorithm() 获得。该方法不需要参数,并且它会返回 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");
}

更新于: 30-Jul-2019

66 次查看

开启你的职业生涯

完成课程,获得认证

开始学习
广告