AlgorithmParameterGenerator getInstance() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getInstance() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // Getting instance of
            // AlgorithmParameterGenerator
            // By using getInstance() method
            AlgorithmParameterGenerator sr
                = AlgorithmParameterGenerator
                      .getInstance("DSA");
  
            // getting the status of
            // AlgorithmParameterGenerator object
            String str = sr.toString();
  
            // printing the status
            System.out.println("Status: "
                               + str);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown: "
                               + e);
        }
        catch (NullPointerException e) {
  
            System.out.println("Exception thrown: "
                               + e);
        }
    }
}

chevron_right


Output:

Status: java.security.AlgorithmParameterGenerator@232204a1

Example 2: To show NoSuchAlgorithmException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getInstance() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // Getting instance of
            // AlgorithmParameterGenerator
            // By using getInstance() method
            AlgorithmParameterGenerator sr
                = AlgorithmParameterGenerator
                      .getInstance("GFG");
  
            // getting the status of
            // AlgorithmParameterGenerator object
            String str = sr.toString();
  
            // printing the status
            System.out.println("Status: " + str);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown: "
                               + e);
        }
        catch (NullPointerException e) {
  
            System.out.println("Exception thrown: "
                               + e);
        }
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getInstance() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // Getting instance of
            // AlgorithmParameterGenerator
            // By using getInstance() method
            AlgorithmParameterGenerator sr
                = AlgorithmParameterGenerator
                      .getInstance("DSA");
  
            // creating Provider object
            Provider pd = sr.getProvider();
  
            // getting algorithm name
            // by using getAlgorithm() mathod
            String algo = sr.getAlgorithm();
  
            // Getting instance of
            // AlgorithmParameterGenerator
            // By using getInstance() method
            AlgorithmParameterGenerator sr1
                = AlgorithmParameterGenerator
                      .getInstance(algo, pd);
  
            // getting the status of
            // AlgorithmParameterGenerator object
            String str = sr1.toString();
  
            // printing the status
            System.out.println("Status: "
                               + str);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown: "
                               + e);
        }
        catch (IllegalArgumentException e) {
  
            System.out.println("Exception thrown: "
                               + e);
        }
    }
}

chevron_right


Output:

Status: java.security.AlgorithmParameterGenerator@232204a1

Example 2: To show NoSuchAlgorithmException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getInstance() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // Getting instance of
            // AlgorithmParameterGenerator
            // By using getInstance() method
            AlgorithmParameterGenerator sr
                = AlgorithmParameterGenerator
                      .getInstance("GFG");
  
            // creating Provider object
            Provider pd = sr.getProvider();
  
            // getting algorithm name
            // by using getAlgorithm() mathod
            String algo = sr.getAlgorithm();
  
            // Getting instance of
            // AlgorithmParameterGenerator
            // By using getInstance() method
            AlgorithmParameterGenerator sr1
                = AlgorithmParameterGenerator
                      .getInstance(algo, pd);
  
            // getting the status of
            // AlgorithmParameterGenerator object
            String str = sr1.toString();
  
            // printing the status
            System.out.println("Status: " + str);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown: "
                               + e);
        }
        catch (IllegalArgumentException e) {
  
            System.out.println("Exception thrown: "
                               + e);
        }
    }
}

chevron_right


Output:

Exception thrown:
 java.security.NoSuchAlgorithmException:
 GFG AlgorithmParameterGenerator not available

Reference:

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : Akanksha_Rai