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 次檢視

開啟你的職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.