A Comprehensive Guide to Extracting RSA Public Key from .cer Certificate and Saving as .pem Using OpenSSL

Dec 07, 2025 · Programming · 7 views · 7.8

Keywords: OpenSSL | RSA Public Key | Certificate Extraction | PEM Format | Encryption Technology

Abstract: This article provides a detailed explanation of how to extract an RSA public key from a DER-encoded .cer certificate file and convert it to PEM format for use with JavaScript encryption libraries. Through OpenSSL command-line tools, we demonstrate the complete workflow from certificate conversion to public key extraction, including command parameter analysis, output format specifications, and practical application scenarios. The article also delves into the differences between certificates and public keys, the structural characteristics of PEM format, and integration methods across various programming environments.

Fundamental Concepts of Certificates and Public Keys

In the field of digital encryption, certificates and public keys are closely related but have distinct structures and purposes. Certificates typically follow the X.509 standard format, containing metadata such as public keys, holder information, and issuer signatures, while the public key is the core cryptographic component of the certificate. Common certificate file formats include .cer (often DER-encoded) and .pem (Base64-encoded text format). Understanding this distinction is crucial for correctly extracting and using encryption keys.

Core Commands of the OpenSSL Toolchain

OpenSSL offers powerful command-line tools for handling various cryptographic operations. To extract a public key from a .cer certificate, the x509 subcommand is used, which is specifically designed for X.509 certificate management. Key parameters include: -inform der specifies the input format as DER encoding, -in specifies the input file, -pubkey indicates extraction of the public key, and -noout prevents output of the certificate itself. The complete command is as follows:

openssl x509 -inform der -in certificate.cer -pubkey -noout > certificate_publickey.pem

This command generates a PEM file containing the RSA public key, with content starting with -----BEGIN PUBLIC KEY----- and ending with -----END PUBLIC KEY-----, and Base64-encoded public key data in between.

In-Depth Analysis of Command Parameters

Let's analyze the role of each parameter in detail: -inform der ensures OpenSSL correctly parses the binary DER format; -pubkey is the key flag that extracts the SubjectPublicKeyInfo structure from the certificate; -noout prevents output of full certificate information, retaining only the public key data. Output redirection (>) saves the result to a specified file instead of displaying it in the terminal. If -noout is omitted, the output includes both the certificate and public key, which does not meet the requirement for pure public key storage.

Structure and Application of PEM Format

The generated PEM file adopts a standard text format, making it easy to use in various programming environments. For example, in JavaScript, the jsencrypt library can load this public key for encryption operations. The essence of PEM format is Base64-encoded binary data with specific header and footer markers, making it readable and transmittable. This format has broad compatibility and can be seamlessly integrated from web applications to server-side scripts.

Practical Application Scenarios and Considerations

In actual development, the extracted public key can be used for front-end encryption, API signature verification, and other scenarios. It is important to ensure that the source certificate file is in valid DER format; otherwise, the command may fail. Additionally, after extraction, the public key should be stored securely to avoid leakage. For batch processing, scripts can be written to automate this process, improving efficiency. By mastering these technical details, developers can handle encryption needs more flexibly and enhance system security.

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.