KeyPairGenerator getAlgorithm() method in Java


The algorithm name for the key pair generator can be obtained using the method getAlgorithm() in the class java.security.KeyPairGenerator. This method requires no parameters and it returns the algorithm name for the key pair generator.

A program that demonstrates this is given as follows −

Example

 Live Demo

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");
      }
   }
}

Output

The Algorithm is: DSA

Now let us understand the above program.

The method getAlgorithm() is used to obtain the algorithm name for the key pair generator. Then this algorithm name is displayed. If the algorithm name is wrong, then the exception NoSuchAlgorithmException is thrown. A code snippet that demonstrates is given as follows −

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");
}

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

43 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements