security

Get bytes of a key pair example

In this example we shall show you how to get the bytes of a key pair in Java. To get the bytes of a key pair one should perform the following steps:

  • Create a KeyPairGenerator for the DSA algorithm and initialize it with a 1024-bit key size.
  • Generate the KeyPair, with the genKeyPair() API method of the KeyPairGenerator.
  • Get the PrivateKey component and the PublicKey component of the key pair, using the getPrivate() and getPublic() API methods of the KeyPair.
  • Get the names of the primary encoding format of both the private and the public key, using the getFormat() API methods of both the PrivateKey and the PublicKey.
  • Get the bytes of the public and private keys, using the getEncoded() API methods of both the PrivateKey and the PublicKey.
  • Get for both the keys their EncodedKeySpec, using the PKCS8EncodedKeySpec for the private key and the X509EncodedKeySpec for the public key.
  • Create a new KeyFactory from the DSA algorithm.
  • Generate a new PrivateKey and a new PublicKey, using the generatePrivate(KeySpec keySpec) and generatePublic(KeySpec keySpec) API methods of the KeyFactory.
  • In order to check if the new keys are equal to the old ones, invoke the equals(Object obj) API method of the String Class.

. Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;
 
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class KeyPairBytesExample {
 
  public static void main(String[] args) {

    try {
	String algorithm = "DSA";  // or RSA, DH, etc.

	// Generate a 1024-bit Digital Signature Algorithm (RSA) key pair
	KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
	keyPairGenerator.initialize(1024);
	KeyPair keyPair = keyPairGenerator.genKeyPair();
	PrivateKey privateKey = keyPair.getPrivate();
	PublicKey publicKey = keyPair.getPublic();

	// Get the formats of the encoded bytes
	String privateKeyFormat = privateKey.getFormat();
	System.out.println("PrivateKey format : " + privateKeyFormat);
	String publicKeyFormat = publicKey.getFormat();
	System.out.println("PublicKey format : " + publicKeyFormat);
	
	// Get bytes of the public and private keys
	byte[] privateKeyBytes = privateKey.getEncoded();
	byte[] publicKeyBytes = publicKey.getEncoded();

	// Get key pair Objects from their respective byte arrays
	// We initialize encoded key specifications based on the encoding formats
	EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
	EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
	
	KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
	PrivateKey newPrivateKey = keyFactory.generatePrivate(privateKeySpec);
	PublicKey newPublicKey = keyFactory.generatePublic(publicKeySpec);

	System.out.println("Is transformation valid ? " + (privateKey.equals(newPrivateKey) && publicKey.equals(newPublicKey)));
	
    } catch (InvalidKeySpecException e) {
    } catch (NoSuchAlgorithmException e) {
    }

 }

}

Output:

PrivateKey format : PKCS#8
PublicKey format : X.509
Is transformation valid ? true

 
This was an example of how to get the bytes of a key pair in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button