Generate public key and private key with OpenSSL in Windows 10

How to get modulus and exponent for RSA public key ?

This tutorial guides you on how to get modulus and exponent for RSA public key using Java. Let’s say you have generated RSA public key and private key using OpenSSL. And now you wanted to get modulus and exponent for the given RSA public key.

Get modulus and exponent for RSA public key

To get modulus and exponent you need to implement the following class “GetModExponentRSAPublicKey.java”

In this utility class, you would be converting the string to public key using the following code as written in getPublicKey() method. For private key you will be using “PKCS8EncodedKeySpec” and for public key you need to use “X509EncodedKeySpec“.

String publicKeyPem = getKey(filename);		
publicKeyPem = publicKeyPem.replace("-----BEGIN PUBLIC KEY-----\n", "");
publicKeyPem = publicKeyPem.replace("-----END PUBLIC KEY-----", "");
byte[] encoded = Base64.decodeBase64(publicKeyPem);	    
X509EncodedKeySpec  keySpec = new X509EncodedKeySpec(encoded);
KeyFactory kf = KeyFactory.getInstance("RSA");	  
PublicKey publicKey = kf.generatePublic(keySpec);

Then you need to type cast with the proper interface RSAPublicKey so that you will get methods getModulus() and getPublicExponent() as shown below.

RSAPublicKey publicKey = (RSAPublicKey) getPublicKey("public.pem");

The below is the complete demo class to get the modulus and exponent for RSA public key.

GetModExponentRSAPublicKey.java

package com.sneppets.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;

import java.security.spec.X509EncodedKeySpec;

import org.apache.commons.codec.binary.Base64;


public class GetModExponentRSAPublicKey {
	
	public static void main (String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
		
				
		RSAPublicKey publicKey = (RSAPublicKey) getPublicKey("public.pem");

		BigInteger mod = publicKey.getModulus();
		BigInteger exp = publicKey.getPublicExponent();
		System.out.println("Modulus: " + mod);
		System.out.println("exponent: " + exp);
	}

	private static PublicKey getPublicKey(String filename) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
		
		String publicKeyPem = getKey(filename);
		
		publicKeyPem = publicKeyPem.replace("-----BEGIN PUBLIC KEY-----\n", "");
		publicKeyPem = publicKeyPem.replace("-----END PUBLIC KEY-----", "");
	    byte[] encoded = Base64.decodeBase64(publicKeyPem);	    
	    X509EncodedKeySpec  keySpec = new X509EncodedKeySpec(encoded);
	    KeyFactory kf = KeyFactory.getInstance("RSA");	  
	    PublicKey publicKey = kf.generatePublic(keySpec);
	    return publicKey;
	}

	private static String getKey(String filename) throws IOException {
             // Read public key from file
	    String strPublicKeyPEM = "";
	    BufferedReader br = new BufferedReader(new FileReader(filename));
	    String line;
	    while ((line = br.readLine()) != null) {
	        strPublicKeyPEM += line + "\n";
	    }
	    br.close();
	    return strPublicKeyPEM;
	}
	
	
}

Output: Modulus and Exponent of RSA Public Key

Modulus: (2048 bit)
         256225235526405098726746477481
         284184271968788058029745255822
         133759432928666883862801285729
         384174985837607256318844020476
         501725786862717237239123308210
         243812164534894667229405844641
         911098277823661406862819893291
         154962752958153104149432835280
         085352705165899064079887611551
         502118308761629010797739872873
         205223957020558671913902386205
         317716646222503466505036050069
         312863533487149618294670795931
         929436739841510971098140145690
         563655049071109366377078390473
         139881492499855404045752443798
         123422594825079544315401608665
         466677213546598874201390156548
         448867823678383666988287958079
         066965097160765227081562608502
         56749742657872987

Exponent: 65537 (0x10001)

Get modulus and exponent for RSA public key – BouncyCastle library

There is another way to get modulus and exponent i.e., by using BouncyCastle library. If you wanted to deal with public.pem file handling, I would recommend you to add the following external library (BouncyCastle) in your build path.

https://mvnrepository.com/artifact/org.bouncycastle/bcprov-ext-jdk16/1.46

First, you need to create the PemFile utility class to handle pem file I/O operations as shown below in your project folder.

PemFile.java

package com.sneppets.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;

public class PemFile {
	
	private PemObject pemObject;
	 
	public PemFile(String filename) throws FileNotFoundException, IOException {
		PemReader pemReader = new PemReader(new InputStreamReader(new FileInputStream(filename)));
		try {
			this.pemObject = pemReader.readPemObject();
		} finally {
			pemReader.close();
		}
	}
 
	public PemObject getPemObject() {
		return pemObject;
	}

}

Then let’s modify our getPublic() method of GetModExponentRSAPublicKey.java class as shown below

private static PublicKey getPublicKey(String filename) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    
   PemFile pemFile = new PemFile(filename);
   byte[] encoded = pemFile.getPemObject().getContent();
   X509EncodedKeySpec  keySpec = new X509EncodedKeySpec(encoded);
   KeyFactory kf = KeyFactory.getInstance("RSA");	  
   PublicKey publicKey = kf.generatePublic(keySpec);
   
   return publicKey;
}

In the above method you need to just replace the following lines

String publicKeyPem = getKey(filename);		
publicKeyPem = publicKeyPem.replace("-----BEGIN PUBLIC KEY-----\n", "");
publicKeyPem = publicKeyPem.replace("-----END PUBLIC KEY-----", "");
byte[] encoded = Base64.decodeBase64(publicKeyPem);	    

Replace with following lines of code, which uses our PemFile util class.

PemFile pemFile = new PemFile(filename);
byte[] encoded = pemFile.getPemObject().getContent();

And you can remove the following method getKey() as well.

private static String getKey(String filename) throws IOException {
    // Read key from file
    String strKeyPEM = "";
    BufferedReader br = new BufferedReader(new FileReader(filename));
    String line;
    while ((line = br.readLine()) != null) {
        strKeyPEM += line + "\n";
    }
    br.close();
   return strKeyPEM;
}

The GetModExponentRSAPublicKey.java class will now looks like below.

package com.sneppets.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;

import java.security.spec.X509EncodedKeySpec;

public class GetModExponentRSAPublicKey {
	
	public static void main (String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
		
				
		RSAPublicKey publicKey = (RSAPublicKey) getPublicKey("public.pem");

		BigInteger mod = publicKey.getModulus();
		BigInteger exp = publicKey.getPublicExponent();
		System.out.println("Modulus: " + mod);
		System.out.println("exponent: " + exp);
	}

	private static PublicKey getPublicKey(String filename) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
	    
		PemFile pemFile = new PemFile(filename);
		byte[] encoded = pemFile.getPemObject().getContent();
	    X509EncodedKeySpec  keySpec = new X509EncodedKeySpec(encoded);
	    KeyFactory kf = KeyFactory.getInstance("RSA");	  
	    PublicKey publicKey = kf.generatePublic(keySpec);
	    return publicKey;
	}	
	
}

You will see the same output when you run the above program.

That’s it. Hope it helped 🙂

Also See:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments