Reconstruct RSA public key from exponent and modulus

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

 


AI Summary
gpt-4o-2024-08-06 2024-12-02 02:11:45
This article explains how to reconstruct an RSA public key using a given exponent and modulus. It outlines the process of converting these values to hexadecimal, creating an ASN.1 file, and using OpenSSL to generate the public key in PEM format.
Chrome On-device AI 2024-12-22 15:31:02

Share Article