Open In App

KeyPairGenerator getInstance() method in Java with Examples

getInstance(String algorithm)

The getInstance() method of java.security.KeyPairGenerator class is used to return a KeyPairGenerator object that generates public/private key pairs for the specified algorithm.

This method traverses the list of registered security Providers, starting with the most preferred Provider. A new KeyPairGenerator object encapsulating the KeyPairGeneratorSpi implementation from the first Provider that supports the specified algorithm is returned.



Syntax:

public static KeyPairGenerator 
    getInstance(String algorithm)
        throws NoSuchAlgorithmException

Parameters: This method takes the standard name of Algorithm as a parameter.



Return Value: This method returns the new KeyPairGenerator object.

Exception: This method throws NoSuchAlgorithmException if no Provider supports a Signature implementation for the specified algorithm.

Below are the examples to illustrate the getInstance() method:

Example 1:




// Java program to demonstrate
// getInstance() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating the object of
            // KeyPairGenerator
            // and getting instance
            // using getInstance() method
            KeyPairGenerator sr = KeyPairGenerator.getInstance("DSA");
  
            // getting the Algorithm
            String algo = sr.getAlgorithm();
  
            // printing the algo String
            System.out.println("Algorithm : " + algo);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

Output:
Algorithm : DSA

Example 2: To show NoSuchAlgorithmException




// Java program to demonstrate
// getInstance() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating the object of
            // KeyPairGenerator
            // and getting instance
            // using getInstance() method
  
            System.out.println("Trying to get"
                               + " the instance of unknown Algorithm");
  
            KeyPairGenerator sr = KeyPairGenerator
                                      .getInstance("TAJMAHAL");
  
            // getting the Algorithm
            String algo = sr.getAlgorithm();
  
            // printing the algo String
            System.out.println("Algorithm : " + algo);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

Output:
Trying to get the instance of unknown Algorithm
Exception thrown : java.security.NoSuchAlgorithmException: TAJMAHAL KeyPairGenerator not available

getInstance(String algorithm, Provider provider)

The getInstance() method of java.security.KeyPairGenerator class is used to return a KeyPairGenerator object that generates public/private key pairs for the specified algorithm.
A new KeyPairGenerator object encapsulating the KeyPairGeneratorSpi implementation from the specified Provider object is returned. Note that the specified Provider object does not have to be registered in the provider list.

Syntax:

public static KeyPairGenerator 
    getInstance(String algorithm, Provider provider)
        throws NoSuchAlgorithmException

Parameters: This method takes the following arguments as a parameters:

Return Value: This method returns the new KeyPairGenerator object.

Exception: This method throws following exceptions:

Article Tags :