Saturday, September 6, 2025
HomeLanguagesJavaKeyPairGenerator initialize() method in Java with Examples

KeyPairGenerator initialize() method in Java with Examples

initialize(int keysize)

The initialize() method of java.security.KeyPairGenerator is used to initialize KeyPairGenerator object for further use.

Syntax:  

public void initialize(int keysize)

Parameters: This method seeks keysize of int type as a parameter.
Return Value: This method has nothing to return.
Exception: This method throws InvalidParameterException if the greater or lesser value than the specified criteria is passed.

Note: The following program will not run on the online IDE.

Below are the examples to illustrate the initialize(int keysize) method:

Example 1:  

Java




// Java program to demonstrate
// initialize() method
 
import java.security.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // creating the object of KeyPairGenerator
            KeyPairGenerator kpg
                = KeyPairGenerator
                      .getInstance("RSA");
 
            // initializing with 1024
            kpg.initialize(1024);
 
            // getting key pairs
            // using genKeyPair() method
            KeyPair kp = kpg.genKeyPair();
 
            // printing the Keypair
            System.out.println("Keypair : " + kp);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

Keypair : java.security.KeyPair@12a3a380

 

Example 2: For InvalidParameterException 

Java




// Java program to demonstrate
// initialize() method
 
import java.security.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // creating the object of KeyPairGenerator
            KeyPairGenerator kpg
                = KeyPairGenerator
                      .getInstance("RSA");
 
            // initializing with 1024
            kpg.initialize(-24);
 
            // getting key pairs
            // using genKeyPair() method
            KeyPair kp = kpg.genKeyPair();
 
            // printing the Keypair
            System.out.println("Keypair : " + kp);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (InvalidParameterException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

Exception thrown :
 java.security.InvalidParameterException:
 RSA keys must be at least 512 bits long

 

initialize(int keysize, SecureRandom random)

The initialize() method of java.security.KeyPairGenerator initializes KeyPairGenerator for particular size with SecureRandom object to use further. 

Syntax:  

public void initialize(int keysize,
                       SecureRandom random)

Parameters: This method takes the following arguments as parameters: 

  • size: which is the keysize
  • random: which is the object of SecureRandom type

Return Value: This method provides the object of KeyPairGenerator.
Exception: This method throws InvalidParameterException if the greater or lesser value than the specified criteria is passed.

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

Example 1: 

Java




// Java program to demonstrate
// initialize() method
 
import java.security.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] argv)
        throws Exception
    {
        try {
 
            // creating the object of KeyPairGenerator
            KeyPairGenerator kpg
                = KeyPairGenerator
                      .getInstance("RSA");
 
            // creating the object of SecureRandom
            SecureRandom se
                = SecureRandom.getInstance("SHA1PRNG");
 
            // initializing with 1024
            kpg.initialize(1024, se);
 
            // getting key pairs
            // using genKeyPair() method
            KeyPair kp = kpg.genKeyPair();
 
            // printing the Keypair
            System.out.println("Keypair : " + kp);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (InvalidParameterException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

Keypair : java.security.KeyPair@4e25154f

 

Example 2: For InvalidParameterException 

Java




// Java program to demonstrate
// initialize() method
 
import java.security.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] argv)
        throws Exception
    {
        try {
 
            // creating the object of KeyPairGenerator
            KeyPairGenerator kpg
                = KeyPairGenerator
                      .getInstance("RSA");
 
            // creating the object of SecureRandom
            SecureRandom se
                = SecureRandom.getInstance("SHA1PRNG");
 
            // initializing with -24
            kpg.initialize(-24, se);
 
            // getting key pairs
            // using genKeyPair() method
            KeyPair kp = kpg.genKeyPair();
 
            // printing the Keypair
            System.out.println("Keypair : " + kp);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (InvalidParameterException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

Exception thrown :
 java.security.InvalidParameterException:
 RSA keys must be at least 512 bits long

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/security/KeyPairGenerator.html#initialize-int-java.security.SecureRandom-
 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11804 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6753 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS