Convert PEM-Encoded Public Key to DER-Encoded using OpenSSL

Convert PEM-Encoded Public Key to DER-Encoded using OpenSSL

In the world of cryptography, different systems and applications may require public keys to be encoded in specific formats. One common format is PEM, while another is DER. PEM is a Base64-encoded ASCII format, while DER is a binary format. This tutorial explains how to convert PEM-encoded public key to DER-encoded using OpenSSL.

Let's say we have the following PEM-encoded public key:

-----END PUBLIC KEY-----

The openssl rsa command can be used to convert a PEM-encoded public key to DER-encoded:

openssl rsa -in -pubin -outform DER -out

The meaning of options:

  • -in - specifies the input file, in this case, the PEM-encoded public key file.
  • -pubin - reads public key instead of a private key.
  • -outform DER - specifies the output format as DER.
  • -out - specifies the output file, in this case, the DER-encoded public key file.

If needed, we can also convert the DER-encoded public key back to PEM-encoded using the following command:

openssl rsa -in -pubin -out

This command follows a similar structure, with the input file being the DER-encoded public key and the output file being the PEM-encoded public key.

Leave a Comment

Cancel reply

Your email address will not be published.