Thursday, July 4, 2024
HomeLanguagesJavaAlgorithmParameters getInstance() method in Java with Examples

AlgorithmParameters getInstance() method in Java with Examples

getInstance(String algorithm)

The getInstance() method of java.security.AlgorithmParameters class returns an object of AlgorithmParameters type that applies the assigned AlgorithmParameters algorithm.
Syntax: 
 

public static AlgorithmParameters
  getInstance(String algorithm)
  throws NoSuchAlgorithmException

Parameters: This method seeks the standard Algorithm as a parameter.
Return Value: This method provides a new algorithm parameters object.
Exception: This method throws following exception: 
 

  • NoSuchAlgorithmException:– if no provider is available to support an algorithm parameter spi application for the particular algorithm.
  • NullPointerException:– if the algorithm is null.

Below are the examples to illustrate the getInstance() method:
Example 1: 
 

Java




// 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 AlgorithmParameters
            // and getting instance
            // By using getInstance() method
            AlgorithmParameters sr
                = AlgorithmParameters
                      .getInstance("DES");
 
            // getting the status
            // of AlgorithmParameters 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);
        }
    }
}


Output: 

Status : null

 

Example 2: To show NoSuchAlgorithmException 
 

Java




// 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 AlgorithmParameters
            // and getting instance
            // By using getInstance() method
            AlgorithmParameters sr
                = AlgorithmParameters.getInstance("GFG");
 
            // getting the status
            // of AlgorithmParameters 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);
        }
    }
}


Output: 

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

 

getInstance(String algorithm, Provider provider)

The getInstance() method of java.security.AlgorithmParameters class returns a object of AlgorithmParameters type that applies the assigned algorithmParameters algorithm and assigned provider object.
Syntax: 
 

public static AlgorithmParameters
  getInstance(String algorithm,  Provider provider)
  throws NoSuchAlgorithmException

Parameters: This method seeks the following arguments as a parameters: 
 

  • algorithm: which is the name of the algorithm specified.
  • provider which is the name of the provider specified

Return Value: This method provides a new algorithm parameters object.
Exception: This method throws following exceptions: 
 

  • NoSuchAlgorithmException: if no provider is available to support an algorithm parameter spi application for the particular algorithm.
  • IllegalArgumentException: if the provider is null.
  • NullPointerException: if the algorithm is null

Below are the examples to illustrate the getInstance() method:
Example 1:
 

Java




// Java program to demonstrate
// getInstance() method
 
import java.security.*;
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating AlgorithmParameterGenerator object
            // and getting instance
            // By using getInstance() method
            AlgorithmParameterGenerator sr
                = AlgorithmParameterGenerator
                      .getInstance("DiffieHellman");
 
            // creating Provider object
            Provider pd = sr.getProvider();
 
            // getting algorithm name
            // by using getAlgorithm() method
            String algo = sr.getAlgorithm();
 
            // creating AlgorithmParameterGenerator object
            // and getting instance
            // 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);
        }
    }
}


Output: 

Status : java.security.AlgorithmParameterGenerator@63947c6b

 

Example 2: To show NoSuchAlgorithmException
 

Java




// Java program to demonstrate
// getInstance() method
 
import java.security.*;
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating AlgorithmParameterGenerator object
            // and getting instance
            // By using getInstance() method
            AlgorithmParameterGenerator sr
                = AlgorithmParameterGenerator
                      .getInstance("DSA");
 
            // creating Provider object
            Provider pd = sr.getProvider();
 
            // getting algorithm name
            // by using getAlgorithm() method
            String algo = sr.getAlgorithm();
 
            // creating AlgorithmParameterGenerator object
            // and getting instance
            // By using getInstance() method
            AlgorithmParameterGenerator sr1
                = AlgorithmParameterGenerator
                      .getInstance("GFG", 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);
        }
    }
}


Output: 

Exception thrown :
 java.security.NoSuchAlgorithmException:
 no such algorithm: GFG for provider SUN

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/security/AlgorithmParameters.html
 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments