KeyFactory getInstance() method in Java with Examples
Last Updated :
20 Dec, 2022
getInstance(String algorithm)
The getInstance() method of java.security.KeyFactory class returns a object of KeyFactory type that applies the assigned KeyFactory algorithm. Syntax:
public static KeyFactory
getInstance(String algorithm)
throws NoSuchAlgorithmException
Parameters: This method seeks the standard Algorithm as a parameter whose instance is to be created to this KeyFactory instance. Return Value: This method returns a new key factory object. Exception: This method throws following exception:
- NoSuchAlgorithmException: if no provider is available to support an key factory spi application for the particular algorithm.
- NullPointerException: if algorithm is null.
Below are the examples to illustrate the getInstance() method: Example 1:
Java
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
KeyFactory sr
= KeyFactory.getInstance("DSA");
String str = sr.getAlgorithm();
System.out.println("algorithm : " + str);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
}
}
|
Example 2: To show NoSuchAlgorithmException
Java
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
KeyFactory sr = KeyFactory.getInstance("GEEKS");
String str = sr.getAlgorithm();
System.out.println("algorithm : " + str);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
}
}
|
Output:
Exception thrown :
java.security.NoSuchAlgorithmException:
GEEKS KeyFactory not available
getInstance(String algorithm, String provider)
The getInstance() method of java.security.KeyFactory class returns a object of KeyFactory type that applies the assigned KeyFactory algorithm and assigned provider object. Syntax:
public static KeyFactory
getInstance(String algorithm, String provider)
throws NoSuchAlgorithmException
Parameters: This method seeks the following arguments as a parameters:
- algorithm: which is the name of the algorithm specified to get the instance.
- provider: which is the name of the provider to be specified
Return Value: This method returns an new key factory object. Exception: This method throws following exceptions:
- NoSuchAlgorithmException:– if no provider is available to support an key factory spi application for the particular algorithm.
- IllegalArgumentException:– if the provider is null.
- NullPointerException:– if algorithm is null
Below are the examples to illustrate the getInstance() method: Example 1:
Java
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
KeyFactory sr
= KeyFactory.getInstance(
"DSA", "SUN");
String str = sr.getAlgorithm();
System.out.println("algorithm : " + str);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
catch (NoSuchProviderException e) {
System.out.println("Exception thrown : " + e);
}
}
}
|
Example 2: To show NoSuchAlgorithmException
Java
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
KeyFactory sr
= KeyFactory.getInstance(
"RSA", "SUN");
String str = sr.getAlgorithm();
System.out.println("algorithm : " + str);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
catch (NoSuchProviderException e) {
System.out.println("Exception thrown : " + e);
}
}
}
|
Output:
Exception thrown :
java.security.NoSuchAlgorithmException:
no such algorithm: RSA for provider SUN
Reference: https://docs.oracle.com/javase/9/docs/api/java/security/KeyFactory.html#getInstance-java.lang.String-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...