Java 中的 KeyPairGenerator getAlgorithm() 方法


使用類 java.security.KeyPairGenerator 中的 getAlgorithm() 方法可以獲取金鑰對生成器的演算法名稱。此方法不需要引數,並且它返回金鑰對生成器的演算法名稱。

演示此的程式如下 −

示例

 線上演示

import java.security.*;
import java.util.*;
public class Demo {
   public static void main(String[] argv) {
      try {
         KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance("DSA");
         String algorithm = kpGenerator.getAlgorithm();
         System.out.println("The Algorithm is: " + algorithm);
      } catch (NoSuchAlgorithmException e) {
         System.out.println("Error!!! NoSuchAlgorithmException");
      }
   }
}

輸出

The Algorithm is: DSA

現在,讓我們瞭解一下上面的程式。

使用 getAlgorithm() 方法來獲取金鑰對生成器的演算法名稱。然後顯示此演算法名稱。如果演算法名稱錯誤,則會丟擲異常 NoSuchAlgorithmException。演示程式碼如下 −

try {
   KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance("DSA");
   String algorithm = kpGenerator.getAlgorithm();
   System.out.println("The Algorithm is: " + algorithm);
} catch (NoSuchAlgorithmException e) {
   System.out.println("Error!!! NoSuchAlgorithmException");
}

更新時間: 2019 年 7 月 30 日

66 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.