Generating Base64 Public Key

Hello,

I don't know if it is possible to do what I want, so asking will save me some time. Maybe I am not understanding this correctly.

I have the exponent and the modulus of a RSA public Key. I have the values in hexadecimal.

Is it possible to create a public key .pem file (in Base64) from this to values? I would like to use this public key with openssl, and it accepts public keys in this format.

Thank you.

[444 byte] By [rafa_lla] at [2007-10-3 4:28:03]
# 1

Here is a complete example

import java.io.*;

import java.math.BigInteger;

import java.security.*;

import java.security.interfaces.RSAPublicKey;

import java.security.spec.*;

public class MakePEMFromRSAPubkey

{

final private BigInteger modulus;

final private BigInteger pubExponent;

final private KeyFactory kf;

final private static String PEM_HEADER = "--BEGIN PUBLIC KEY--";

final private static String PEM_FOOTER = "--END PUBLIC KEY--";

public MakePEMFromRSAPubkey(final BigInteger modulus,

final BigInteger pubExponent) throws NoSuchAlgorithmException

{

this.modulus = modulus;

this.pubExponent = pubExponent;

kf = KeyFactory.getInstance("RSA");

}

public static void main(String[] args) throws InvalidKeySpecException,

NoSuchAlgorithmException, FileNotFoundException

{

BigInteger modulus = new BigInteger(

"DEA1A28CCAF2A670BCCCB567CAD3B44CBC420858FE7CC3DA187AE6BA9BDF3BAD3C68D203AE219895FB1CC19383F0E2662E09CDA6486F140DB39C81AC538940AB",

16);

BigInteger pubExponent = new BigInteger("10001", 16); // 65537

final String pemFileName = "example.pem";

MakePEMFromRSAPubkey keyMaker = new MakePEMFromRSAPubkey(modulus,

pubExponent);

keyMaker.writeKey(pemFileName);

}

private void writeKey(String pemFileName) throws InvalidKeySpecException,

FileNotFoundException

{

RSAPublicKeySpec rsaKS = new RSAPublicKeySpec(modulus, pubExponent);

RSAPublicKey rsaPubKey = (RSAPublicKey) kf.generatePublic(rsaKS);

byte[] encoded = rsaPubKey.getEncoded();

sun.misc.BASE64Encoder b64 = new sun.misc.BASE64Encoder();

String base64 = b64.encode(encoded);

PrintStream ps = new PrintStream(new FileOutputStream(pemFileName));

ps.println(PEM_HEADER);

ps.println(base64);

ps.println(PEM_FOOTER);

ps.close();

}

}

And some commands (DOS/Windows) showing the result

> type example.pem

--BEGIN PUBLIC KEY--

MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAN6hoozK8qZwvMy1Z8rTtEy8QghY/nzD2hh65rqb3zut

PGjSA64hmJX7HMGTg/DiZi4JzaZIbxQNs5yBrFOJQKsCAwEAAQ==

--END PUBLIC KEY--

> openssl rsa -in example.pem -pubin -text -noout

Modulus (512 bit):

00:de:a1:a2:8c:ca:f2:a6:70:bc:cc:b5:67:ca:d3:

b4:4c:bc:42:08:58:fe:7c:c3:da:18:7a:e6:ba:9b:

df:3b:ad:3c:68:d2:03:ae:21:98:95:fb:1c:c1:93:

83:f0:e2:66:2e:09:cd:a6:48:6f:14:0d:b3:9c:81:

ac:53:89:40:ab

Exponent: 65537 (0x10001)

ghstarka at 2007-7-14 22:30:59 > top of Java-index,Security,Cryptography...
# 2
Thank you very much for your answer. I'll take a look of it and try it.
rafa_lla at 2007-7-14 22:30:59 > top of Java-index,Security,Cryptography...