getInstance(String algorithm)
The getInstance() method of java.security.AlgorithmParameterGenerator class is used to return an object of AlgorithmParameterGenerator type that implements the specified algorithm.
Syntax:
public static AlgorithmParameterGenerator
getInstance(String algorithm)
throws NoSuchAlgorithmException
Parameters: This method takes the standard name of Algorithm as a parameter.
Return Value: This method returns the new AlgorithmParameterGenerator object.
Exception: This method throws following exception:
- NoSuchAlgorithmException: if no Provider supports an AlgorithmParameterGeneratorSpi implementation for the specified algorithm.
- NullPointerException: if the specified 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 {
AlgorithmParameterGenerator sr
= AlgorithmParameterGenerator
.getInstance( "DSA" );
String str = sr.toString();
System.out.println( "Status: "
+ str);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown: "
+ e);
}
catch (NullPointerException e) {
System.out.println( "Exception thrown: "
+ e);
}
}
}
|
Output:
Status: java.security.AlgorithmParameterGenerator@232204a1
Example 2: To show NoSuchAlgorithmException
Java
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
AlgorithmParameterGenerator sr
= AlgorithmParameterGenerator
.getInstance( "GFG" );
String str = sr.toString();
System.out.println( "Status: " + 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:
GFG AlgorithmParameterGenerator not available
Signature getInstance(String algorithm, Provider provider)
The getInstance() method of java.security.AlgorithmParameterGenerator class is used to return a AlgorithmParameterGenerator object that implements the specified signature algorithm and specified provider object.
Syntax:
public static AlgorithmParameterGenerator
getInstance(String algorithm, Provider provider)
throws NoSuchAlgorithmException
Parameters: This method takes the following arguments as a parameters:
- algorithm: which is the name of the algorithm requested.
- provider: which is the provider specified
Return Value: This method returns the new AlgorithmParameterGenerator object.
Exception: This method throws following exceptions:
- NoSuchAlgorithmException: if a AlgorithmParameterGeneratorSpi implementation for the specified algorithm is not available from the specified Provider object.
- IllegalArgumentException: if the provider is null.
- NullPointerException: if the 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 {
AlgorithmParameterGenerator sr
= AlgorithmParameterGenerator
.getInstance( "DSA" );
Provider pd = sr.getProvider();
String algo = sr.getAlgorithm();
AlgorithmParameterGenerator sr1
= AlgorithmParameterGenerator
.getInstance(algo, pd);
String str = sr1.toString();
System.out.println( "Status: "
+ str);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown: "
+ e);
}
catch (IllegalArgumentException e) {
System.out.println( "Exception thrown: "
+ e);
}
}
}
|
Output:
Status: java.security.AlgorithmParameterGenerator@232204a1
Example 2: To show NoSuchAlgorithmException
Java
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
AlgorithmParameterGenerator sr
= AlgorithmParameterGenerator
.getInstance( "GFG" );
Provider pd = sr.getProvider();
String algo = sr.getAlgorithm();
AlgorithmParameterGenerator sr1
= AlgorithmParameterGenerator
.getInstance(algo, pd);
String str = sr1.toString();
System.out.println( "Status: " + str);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown: "
+ e);
}
catch (IllegalArgumentException e) {
System.out.println( "Exception thrown: "
+ e);
}
}
}
|
Output:
Exception thrown:
java.security.NoSuchAlgorithmException:
GFG AlgorithmParameterGenerator not available
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
03 Jun, 2021
Like Article
Save Article