Extracting Exponent and Modulus from an RSA Public Key: A Detailed Guide

Dec 08, 2025 · Programming · 15 views · 7.8

Keywords: RSA | PublicKey | Exponent | Modulus | OpenSSL

Abstract: This article provides a comprehensive guide on how to retrieve the public exponent and modulus from an RSA public key file, focusing on command-line methods using OpenSSL and Java approaches, with step-by-step instructions and key considerations for developers and cryptography enthusiasts.

Introduction

In RSA encryption, the public key is typically stored in a file containing two core parameters: the public exponent (e) and the modulus (n). Many JavaScript libraries require these parameters separately for encryption, but extracting them from a public.key file can be challenging. This article analyzes the extraction process in depth, based on Q&A data, offering practical solutions.

Extracting via OpenSSL Command Line

If you have command-line access, OpenSSL is a powerful tool. Assume the public key file is in PEM format, as shown below:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmBAjFv+29CaiQqYZIw4P
J0q5Qz2gS7kbGleS3ai8Xbhu5n8PLomldxbRz0RpdCuxqd1yvaicqpDKe/TT09sR
mL1h8Sx3Qa3EQmqI0TcEEqk27Ak0DTFxuVrq7c5hHB5fbJ4o7iEq5MYfdSl4pZax
UxdNv4jRElymdap8/iOo3SU1RsaK6y7kox1/tm2cfWZZhMlRFYJnpoXpyNYrp+Yo
CNKxmZJnMsS698kaFjDlyznLlihwMroY0mQvdD7dCeBoVlfPUGPAlamwWyqtIU+9
5xVkSp3kxcNcNb/mePSKQIPafQ1sAmBKPwycA/1I5nLzDVuQa95ZWMn0JkphtFIh
HQIDAQAB
-----END PUBLIC KEY-----

First, remove the header and footer from the file and decode the Base64 content. Use the following command:

PUBKEY=`grep -v -- ----- public.key | tr -d '\n'`

Then, parse the ASN.1 structure to locate the exponent and modulus:

echo $PUBKEY | base64 -d | openssl asn1parse -inform DER -i

The output will show a hierarchical structure, with the exponent and modulus typically in a BIT STRING. Use the -strparse parameter for further extraction:

echo $PUBKEY | base64 -d | openssl asn1parse -inform DER -i -strparse 19

This outputs two INTEGER values: the modulus and exponent, in hexadecimal. For example, the modulus may be a long string, and the exponent is often 65537 (0x10001).

A simpler method is to use an OpenSSL command for formatted output:

openssl rsa -pubin -inform PEM -text -noout < public.key

This command directly displays the detailed values of the modulus and exponent, suitable for scripting.

Java Approach

In a Java environment, you can use libraries like BouncyCastle to read the public key file. Assuming the file is in PEM format, a code example is as follows: first, parse the file via PEMReader, then check if it is an instance of RSAPublicKey to retrieve the parameters.

PEMReader pemReader = new PEMReader(new FileReader("file.pem"));
Object obj = pemReader.readObject();
pemReader.close();
if (obj instanceof RSAPublicKey) {
    RSAPublicKey rsaPubKey = (RSAPublicKey) obj;
    BigInteger modulus = rsaPubKey.getModulus();
    BigInteger exponent = rsaPubKey.getPublicExponent();
    // Use modulus and exponent for further operations
}

This method is flexible and applicable to various public key formats, including X.509 certificates.

Important Notes

When extracting via the OpenSSL command line, note that the modulus output may include leading zero bytes (e.g., 00), due to its representation as a signed integer. In practice, this can cause length mismatches; it is recommended to handle or ignore these zeros in scripts. For instance, when using extracted values in JavaScript, verify the binary format to avoid errors.

In summary, extracting the exponent and modulus from an RSA public key can be achieved through command-line tools like OpenSSL or programming libraries like BouncyCastle, with key steps involving parsing ASN.1 structures and addressing potential data format issues.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.