KeyFactory generatePrivate() method in Java with Examples

The generatePrivate() method of java.security.KeyPairGenerator class is used to generates a private key object from the provided key specification (key material).

Syntax:

public final PrivateKey generatePrivate(KeySpec keySpec)
                                 throws InvalidKeySpecException

Parameters: This method takes keySpec(the specification (key material) of the private key) as a parameter.



Return Value: This method returns the private key.

Exception: This method throws InvalidKeySpecException if the given key specification is inappropriate for this key factory to produce a private key.

Below are the examples to illustrate the generatePrivate() method

Note: The following program will not run on online IDE

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// generatePrivate() method
  
import java.security.*;
import java.util.*;
import java.security.spec.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating the object of KeyPairGenerator
            KeyPairGenerator kpg = KeyPairGenerator
                                       .getInstance("DSA");
  
            // initializing with 1024
            kpg.initialize(1024);
  
            // getting key pairs
            // using generateKeyPair() method
            KeyPair kp = kpg.genKeyPair();
  
            // getting public key
            PrivateKey prv = kp.getPrivate();
  
            // getting byte data of private key
            byte[] private KeyBytes = prv.getEncoded();
  
            // creating keyspec object
            EncodedKeySpec
                private KeySpec
                = new PKCS8EncodedKeySpec(private KeyBytes);
  
            // creating object of keyfactory
            KeyFactory keyFactory = KeyFactory.getInstance("DSA");
  
            // generating private key from the provided key spec.
            // using generatePrivate() method
            PrivateKey private Key = keyFactory
                                        .generatePrivate(private KeySpec);
  
            // printing private key
            System.out.println("PrivateKey : " + private Key);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

PrivateKey : sun.security.provider.DSAPrivateKey@fff96ed9

Example 2: For InvalidKeySpecException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// generatePrivate() method
  
import java.security.*;
import java.util.*;
import java.security.spec.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating the object of KeyPairGenerator
            KeyPairGenerator kpg = KeyPairGenerator
                                       .getInstance("DSA");
  
            // initializing with 1024
            kpg.initialize(1024);
  
            // getting key pairs
            // using generateKeyPair() method
            KeyPair kp = kpg.genKeyPair();
  
            // getting public key
            PrivateKey prv = kp.getPrivate();
  
            // getting byte data of private key
            byte[] private KeyBytes = prv.getEncoded();
  
            // creating keyspec object
            EncodedKeySpec
                private KeySpec
                = new X509EncodedKeySpec(private KeyBytes);
  
            // creating object of keyfactory
            KeyFactory keyFactory = KeyFactory.getInstance("DSA");
  
            // generating private key from the provided key spec.
            // using generatePrivate() method
            PrivateKey private Key = keyFactory
                                        .generatePrivate(private KeySpec);
  
            // printing private key
            System.out.println("Private Key : " + private Key);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (InvalidKeySpecException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Exception thrown : java.security.spec.InvalidKeySpecException:
 Inappropriate key specification

java-img




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