Skip to content
Related Articles

Related Articles

AlgorithmParameterGenerator init() method in Java with Examples
  • Last Updated : 14 Oct, 2019
GeeksforGeeks - Summer Carnival Banner

init(int size)

The init() method of java.security.AlgorithmParameterGenerator class is used to initialize AlgorithmParameterGenerator for particular size to use further.

Syntax:

public final void init(int size)

Parameters: This method takes size of int type as a parameter.

Return Value: This method returns nothing.

Below are the examples to illustrate the init(int size) method:



Example 1:




// Java program to demonstrate
// init() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating the object of
            // AlgorithmParameterGenerator
            // and getting instance
            // using getInstance() method
            AlgorithmParameterGenerator
                sr
                = AlgorithmParameterGenerator
                      .getInstance("DSA");
  
            // initializing the AlgorithmParameterGenerator
            // with 1024 using initialize() method
            sr.init(1024);
  
            // genrating the Parameters
            // using generateParameters() method
            AlgorithmParameters param
                = sr.generateParameters();
  
            // printing the keypair
            System.out.println("AlgorithmParameters : "
                               + param);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}
Output:

AlgorithmParameters :
p: a695be97 15fe7cdf 3f9b4e5f a2b640cf ecec7852 2f3208dc 941187c5 4be1a391
cfe13145 62310769 17b051e2 4fa62871 4aefada2 e0dbb0fe b0987482 9cb9a290
09e340dd 61a9bc49 34175131 afc3d452 d8d6a439 de1ed044 3dfeeda9 32741d06
17d490b7 23a31141 0a4c09d6 7f69d68e 59f7b912 81f20d7f 9f6ede2c 504e267f
q: a85d0845 5d188578 b39074d9 8797be08 9620d4f9
g: 9abd9824 d319bf88 381f4f88 4f83282c ae3555e6 a91d2a94 d0b9c18e 3d82b1a6
412acd42 a7a2e3b7 f524ffaf 8843fe1c d6845c3c ec240815 33d26dd5 af457b65
aa1f670d 6ade4789 2c504a21 67e781c9 c1797a7a 1e39eeae 8da5433b bdbffbdd
a64efcfc 209f25f4 c162d88a d3f9b4f9 f0a247b8 a6b97c1c aec2ac91 b8bb279b

init(int size, SecureRandom random)

The init() method of java.security.AlgorithmParameterGenerator class is used to initialize AlgorithmParameterGenerator for particular size with SecureRandom object to use further .

Syntax:

public final void init(int size,
                       SecureRandom random)

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

  • size which is the size to be specified for initialization
  • random which is the object of SecureRandom type to be specified to this AlgorithmParameterGenerator object

Return Value: This method returns the new AlgorithmParameterGenerator object.

Below are the examples to illustrate the init() method:

Example 1:




// Java program to demonstrate
// init() method
  
import java.security.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating the object of
            // AlgorithmParameterGenerator
            // and getting instance
            // using getInstance() method
            AlgorithmParameterGenerator
                sr
                = AlgorithmParameterGenerator
                      .getInstance("DSA");
  
            // creating the object of SecureRandom
            SecureRandom se
                = SecureRandom.getInstance("SHA1PRNG");
  
            // initializing the AlgorithmParameterGenerator
            // with 1024 and SecureRandom object
            // using init() method
            sr.init(1024, se);
  
            // genrating the Parameters
            // using generateParameters() method
            AlgorithmParameters param
                = sr.generateParameters();
  
            // printing the keypair
            System.out.println("AlgorithmParameters : "
                               + param);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}
Output:

AlgorithmParameters :
p: b1da365d af3829d0 11eb4e13 3bd01aef 3c25e6e3 1c6f993b 5633a4bf 81ca9680
587717e4 08c3a9e7 ead7b7a1 ff02561f efd2efa0 601c3f4c 90080ad0 ec29f3a9
3e167027 c7a2b3f8 a04c79a8 837f9be9 7731f2d2 499eb86c 37612953 32b5ad71
cbbf5bf4 6407f18a fd716be0 706e1717 e7357af9 1c1bee1e fa9c5cf7 f3506a75
q: bf8d23a2 5d590c5c fb6b6df2 bb104a06 c1492a3b
g: 3510f6d8 a55b2c00 645e1dca 66448d2a 7d8e54d3 5caa67b9 00de36d2 518c0a40
985433c3 be45c8ed 2dc04c61 173c663f 030290fe 0158ef11 3642e12f f243ac6c
2bbe2a83 80a1f1ae 4a750d3e eab3bc27 bb56a7d1 12c6676a 6142a380 8a5ca7d9
f07375e6 56c33723 5de931e7 0339f95a bd9cbd18 eebed3bc d7f6365b 235f7d65

Reference:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :