KeyFactory getAlgorithm() method in Java

Java 8ProgrammingObject Oriented Programming

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

A program that demonstrates this is given as follows −

Example

 Live Demo

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

Output

The Algorithm is: RSA

Now let us understand the above program.

The method getAlgorithm() is used to obtain the algorithm name for the KeyFactory. 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 {
   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");
}
raja
Updated on 30-Jul-2019 22:30:25

Advertisements