KeyPairGenerator getProvider() method in Java


The provider of the key pair object can be obtained using the getProvider() method in the class java.security.KeyPairGenerator. This method requires no parameters and it returns the provider of the key pair object.

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");
         Provider p = kpGenerator.getProvider();
         System.out.println("The Provider is: " + p.getName());
      } catch (NoSuchAlgorithmException e) {
         System.out.println("Error!!! NoSuchAlgorithmException");
      }
   }
}

Output

The Provider is: SUN

Now let us understand the above program.

The getProvider() method is used to get the provider of the key pair object. Then the provider 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");
   Provider p = kpGenerator.getProvider();
   System.out.println("The Provider is: " + p.getName());
} catch (NoSuchAlgorithmException e) {
   System.out.println("Error!!! NoSuchAlgorithmException");
}

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

43 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements