KeyFactory getInstance() method in Java with Examples
getInstance(String algorithm)
The getInstance() method of java.security.KeyFactory class returns a object of KeyFactory type that applys 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 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 KeyFactory // and getting instance // By usng getInstance() method KeyFactory sr = KeyFactory.getInstance("DSA"); // getting the algoritm of KeyFactory object String str = sr.getAlgorithm(); // printing the status System.out.println("algorithm : " + str); } catch (NoSuchAlgorithmException e) { System.out.println("Exception thrown : " + e); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
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 KeyFactory // and getting instance // By usng getInstance() method KeyFactory sr = KeyFactory.getInstance("GEEKS"); // getting the algoritm of KeyFactory object String str = sr.getAlgorithm(); // printing the status System.out.println("algorithm : " + str); } catch (NoSuchAlgorithmException e) { System.out.println("Exception thrown : " + e); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
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 applys 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 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 KeyFactory // and getting instance // By usng getInstance() method KeyFactory sr = KeyFactory.getInstance( "DSA", "SUN"); // getting the algoritm of KeyFactory object String str = sr.getAlgorithm(); // printing the status 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); } } } |
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 KeyFactory // and getting instance // By usng getInstance() method KeyFactory sr = KeyFactory.getInstance( "RSA", "SUN"); // getting the algoritm of KeyFactory object String str = sr.getAlgorithm(); // printing the status 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); } } } |
Exception thrown : java.security.NoSuchAlgorithmException: no such algorithm: RSA for provider SUN
Recommended Posts:
- KeyFactory generatePublic() method in Java with Examples
- KeyFactory getProvider() method in Java with Examples
- KeyFactory getAlgorithm() method in Java with Examples
- KeyFactory generatePrivate() method in Java with Examples
- NumberFormat getInstance() method in Java with Examples
- DecimalFormatSymbols getInstance() method in Java with Examples
- Currency getInstance() Method in Java with Examples
- SecureRandom getInstance() method in Java with Examples
- Java Signature getInstance() method with Examples
- AlgorithmParameters getInstance() method in Java with Examples
- Calendar getInstance() Method in Java with Examples
- MessageDigest getInstance() method in Java with Examples
- KeyPairGenerator getInstance() method in Java with Examples
- AlgorithmParameterGenerator getInstance() method in Java with Examples
- DecimalFormatSymbols getInstance(Locale) method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



