AlgorithmParameterGenerator getAlgorithm() 方法在 Java 中


可以使用 類 java.security.AlgorithmParameterGenerator 中的方法 getAlgorithm() 獲取引數生成器的演算法名稱。此方法不需要任何引數,它以字串形式返回演算法名稱。

演示此方法的程式如下所示 −

示例

 線上演示

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

輸出

The Algorithm is: DiffieHellman

現在讓我們理解上述程式。

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

try {
   AlgorithmParameterGenerator apGenerator = AlgorithmParameterGenerator.getInstance("DiffieHellman");
   apGenerator.init(1024);
   String algorithm = apGenerator.getAlgorithm();
   System.out.println("The Algorithm is: " + algorithm);
} catch (NoSuchAlgorithmException e) {
   System.out.println("Error!!! NoSuchAlgorithmException");
} catch (ProviderException e) {
   System.out.println("Error!!! ProviderException");
}

更新於: 30-Jul-2019

46 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

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