Generate public key and private key with OpenSSL in Windows 10

Java: Convert String to RSA Public Key

This tutorial guides you on how to convert string to RSA public key. I have generated public key and private key .pem files using OpenSSL using this tutorial. Let’s see how to read key string from public.pem file and convert to public key.

Convert String to RSA Public Key

The following is the example program to convert string to RSA private key i.e., key in string format to private key.

ConvertStringToKey.java

package com.sneppets.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;

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

public class ConvertStringToKey {
	
	public static void main (String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
		
		String publicKeyPemStr = getKey("public.pem");
		System.out.println("Public Key String: ");
		System.out.println("***************************");
		System.out.println(publicKeyPemStr);
		
		publicKeyPemStr = publicKeyPemStr.replace("-----BEGIN PUBLIC KEY-----\n", "");
		publicKeyPemStr = publicKeyPemStr.replace("-----END PUBLIC KEY-----", "");
	    byte[] encoded = Base64.decodeBase64(publicKeyPemStr);
	    
	    X509EncodedKeySpec  keySpec = new X509EncodedKeySpec(encoded);
	    KeyFactory keyFactory = KeyFactory.getInstance("RSA");	  
	    PublicKey publicKey = keyFactory.generatePublic(keySpec);
	    System.out.println("String to Public Key: ");
	    System.out.println("***************************");
	    System.out.println(publicKey);
	    
	}

	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;
	}

}

Output

Public Key String:
***************************
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyvgwEYZlJR5Xv6dPlFpg
hG7o9IaXxuLB+b/qsghkLG1iCF2RZebp699BjMkbJs1KQq3F3pOFHxqd1kBIl7zk
fXDzUIw475SWKQ2LWSiD+7WV04t8RNLIAi9JsJdfLl3Unfc+bTqgsSWHcLeP0lEu
GG+bvocbpx8NXPvizXyyOT+bGGmdqEhcqitYhM5B4C82cTY+XnQ4q/zJK4pm9bGr
ycJX4dzWjQdAPt65BKkXWsd7sgy0DI8nTSliJCdClEvysOVt8RDHQ6CoL70CA1LT
2aLD7x73G2h7uksTQ0ztHyxUdaX6G+slg+BwJgEn8p+o6qq4OQUJE0YISfMd99Ok
WwIDAQAB
-----END PUBLIC KEY-----

String to Public Key:
***************************
Sun RSA public key, 2048 bits
params: null
modulus: 25622523552640509872674647748128418427196878805802974525582213375943292866688386280128572938417498583760725631884402047650172578686271723723912330821024381216453489466722940584464191109827782366140686281989329115496275295815310414943283528008535270516589906407988761155150211830876162901079773987287320522395702055867191390238620531771664622250346650503605006931286353348714961829467079593192943673984151097109814014569056365504907110936637707839047313988149249985540404575244379812342259482507954431540160866546667721354659887420139015654844886782367838366698828795807906696509716076522708156260850256749742657872987
public exponent: 65537

That’s it. Hope it helped 🙂

Also See:

References:

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Eduardo
Eduardo
2 years ago

donde iría el archivo public.pem?