The RSA encryption algorithm uses a pair of public key and private key to implementing the security mechanism. Public key is used to encrypt information and private key is used to decrypt the information and vice versa. Java provides us with several classes to work with the RSA algorithm in java.security package. In this tutorial, I show you how to use Java Security’s KeyPairGenerator class to generate a public key and private key pair for your use!
I will create a main class as an example as follows:
1 2 3 4 5 6 7 8 9 |
package com.huongdanjava.javaexample; public class Example { public static void main(String[] args) { } } |
First, you need to initialize the KeyPairGenerator object using the static getInstance() method with the RSA algorithm and the size 1024 or 2048 as follows:
1 2 |
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(2048); |
Using the generateKeyPair() method of the KeyPairGenerator object, you will generate a pair of public key and private key, the information contained in the KeyPair object:
1 |
KeyPair keyPair = kpg.generateKeyPair(); |
From this KeyPair object, you can get the object containing the public key and private key as follows:
1 2 |
Key pub = keyPair.getPublic(); Key pvt = keyPair.getPrivate(); |
You can get the format information of the public key and private key as follows:
1 2 3 4 5 |
Key pub = keyPair.getPublic(); System.out.println("Public key format: " + pub.getFormat()); Key pvt = keyPair.getPrivate(); System.out.println("Private key format: " + pvt.getFormat()); |
My results when running are as follows:
As you can see, the default format of the public key is X.509 and the private key is PKCS#8. You can learn more about these formats on the internet!
To save this public key and private key to a file, you can use the following method:
1 2 3 4 5 6 7 8 9 10 |
public void generatePublicKeyAndPrivateKey(KeyPair keypair, String outputFileWithoutExtension) throws IOException { OutputStream out = new FileOutputStream(outputFileWithoutExtension + ".key"); out.write(keypair.getPrivate().getEncoded()); out.close(); out = new FileOutputStream(outputFileWithoutExtension + ".pub"); out.write(keypair.getPublic().getEncoded()); out.close(); } |
My entire example code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.huongdanjava.javaexample; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.security.Key; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; public class Example { public static void main(String[] args) throws NoSuchAlgorithmException, IOException { KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(2048); KeyPair keyPair = kpg.generateKeyPair(); Key pub = keyPair.getPublic(); System.out.println("Public key format: " + pub.getFormat()); Key pvt = keyPair.getPrivate(); System.out.println("Private key format: " + pvt.getFormat()); generatePublicKeyAndPrivateKey(keyPair, "/Users/Khanh/Documents/huongdanjava"); } private static void generatePublicKeyAndPrivateKey(KeyPair keypair, String outputFileWithoutExtension) throws IOException { OutputStream out = new FileOutputStream(outputFileWithoutExtension + ".key"); out.write(keypair.getPrivate().getEncoded()); out.close(); out = new FileOutputStream(outputFileWithoutExtension + ".pub"); out.write(keypair.getPublic().getEncoded()); out.close(); } } |
Please replace the path to the output file to your liking.
Run the example and check the results!