Reconstruct RSA public key from exponent and modulus

(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, replace the yellow color 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
Chrome On-device AI 2024-11-21 20:51:40

Share Article