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:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
KeyPairGenerator sr = KeyPairGenerator.getInstance( "DSA" );
String algo = sr.getAlgorithm();
System.out.println( "Algorithm : " + algo);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (ProviderException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Example 2: To show NoSuchAlgorithmException
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
System.out.println( "Trying to get"
+ " the instance of unknown Algorithm" );
KeyPairGenerator sr = KeyPairGenerator
.getInstance( "TAJMAHAL" );
String algo = sr.getAlgorithm();
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:
- algorithm: the name of the algorithm requested.
- provider: the provider
Return Value: This method returns the new KeyPairGenerator object.
Exception: This method throws following exceptions:
- NoSuchAlgorithmException– if a SignatureSpi implementation for the specified algorithm is not available from the specified Provider object.
- IllegalArgumentException– if the provider is null.
Below are the examples to illustrate the getInstance() method:
Note: The following program will not run on online IDE.
Example 1:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
KeyPairGenerator sr = KeyPairGenerator.getInstance( "DSA" );
Provider pd = sr.getProvider();
String algo = sr.getAlgorithm();
KeyPairGenerator sr1 = KeyPairGenerator.getInstance(algo, pd);
KeyPair keypair = sr1.generateKeyPair();
System.out.println( "Keypair : " + keypair);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (ProviderException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Keypair : java.security.KeyPair@12a3a380
Example 2: To show NoSuchAlgorithmException
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
KeyPairGenerator sr = KeyPairGenerator.getInstance( "DSA" );
Provider pd = sr.getProvider();
KeyPairGenerator sr1 = KeyPairGenerator.getInstance( "TAJMAHAL" , pd);
KeyPair keypair = sr1.generateKeyPair();
System.out.println( "Keypair : " + keypair);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (ProviderException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Exception thrown : java.security.NoSuchAlgorithmException:
no such algorithm: TAJMAHAL for provider SUN
Example 3: To show IllegalArgumentException
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
KeyPairGenerator sr = KeyPairGenerator
.getInstance( "RSA" );
System.out.println( "Trying to assign null as a provider" );
Provider pd = null ;
String algo = sr.getAlgorithm();
KeyPairGenerator sr1 = KeyPairGenerator
.getInstance(algo, pd);
KeyPair keypair = sr1.generateKeyPair();
System.out.println( "Keypair : " + keypair);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (ProviderException e) {
System.out.println( "Exception thrown : " + e);
}
catch (IllegalArgumentException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Trying to assign null as a provider
Exception thrown : java.lang.IllegalArgumentException:
missing provider
Last Updated :
23 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...