Java中的 KeyPairGenerator getAlgorithm() 方法及示例
Java.security.KeyPairGenerator类的getAlgorithm()方法用于返回此密钥对生成器的算法标准名称。有关标准算法名称的信息,请参阅Java Cryptography Architecture Standard Algorithm Name Documentation 中的 KeyPairGenerator 部分。
句法:
public String getAlgorithm()
返回值:该方法返回算法的标准字符串名称。
以下是说明getAlgorithm()方法的示例
示例 1:
Java
// Java program to demonstrate
// genKeyPair() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// creating the object of KeyPairGenerator
KeyPairGenerator kpg = KeyPairGenerator
.getInstance("RSA");
// fetching the Algorithm
// using getAlgorithm() method
String algo = kpg.getAlgorithm();
// printing the string algo
System.out.println("Algorithm : " + algo);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
}
}
Java
// Java program to demonstrate
// genKeyPair() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// creating the object of KeyPairGenerator
KeyPairGenerator kpg = KeyPairGenerator
.getInstance("DiffieHellman");
// fetching the Algorithm
// using getAlgorithm() method
String algo = kpg.getAlgorithm();
// printing the string algo
System.out.println("Algorithm : " + algo);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
Algorithm : RSA
示例 2:
Java
// Java program to demonstrate
// genKeyPair() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// creating the object of KeyPairGenerator
KeyPairGenerator kpg = KeyPairGenerator
.getInstance("DiffieHellman");
// fetching the Algorithm
// using getAlgorithm() method
String algo = kpg.getAlgorithm();
// printing the string algo
System.out.println("Algorithm : " + algo);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
Algorithm : DiffieHellman