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 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:
// 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); } } } |
PrivateKey : sun.security.provider.DSAPrivateKey@fff96ed9
Example 2: For InvalidKeySpecException
// 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); } } } |
Exception thrown : java.security.spec.InvalidKeySpecException: Inappropriate key specification
Recommended Posts:
- KeyFactory getInstance() method in Java with Examples
- KeyFactory getAlgorithm() method in Java with Examples
- KeyFactory generatePublic() method in Java with Examples
- KeyFactory getProvider() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java 8 | ArrayDeque removeIf() method in Java with Examples
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.



