Given its exponent and modulus, this article shows the steps to construct an RSA public key.


1) Convert exponent and modulus to hex if they are not.

echo -n BASE64_VALUE|base64 --decode | xxd -p -u | tr -d \\n

2) Create an asn1 definition file and replace the <HEX_MODULUS_VALUE> and <HEX_EXPONENT_VALUE> with the value obtained in step (1).

# Start with a SEQUENCE
asn1=SEQUENCE:pubkeyinfo

# pubkeyinfo contains an algorithm identifier and the public key wrapped
# in a BIT STRING
[pubkeyinfo]
algorithm=SEQUENCE:rsa_alg
pubkey=BITWRAP,SEQUENCE:rsapubkey

# algorithm ID for RSA is just an OID and a NULL
[rsa_alg]
algorithm=OID:rsaEncryption
parameter=NULL

# Actual public key: modulus and exponent
[rsapubkey]
n=INTEGER:0xHEX_MODULUS_VALUE

e=INTEGER:0xHEX_EXPONENT_VALUE

3) Save the file as "def.asn1"

4) Use OpenSSL to construct DER from ASN1

openssl asn1parse -genconf def.asn1 -out pubkey.der -noout

5) Use OpenSSL to convert DER to PEM format

openssl rsa -in pubkey.der -inform der -pubin -out pubkey.pem

6) Obtain the Public Key in PEM file

cat pubkey.pem