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:

-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAN/ovAmhoNOnE2VoJeUunpEB+CwTyPbK
I8VnQ+rLa75riBtmfYrGdUD8Cv/p+ioL86xwSvX+HYXnkPffEXc7IlUCAwEAAQ==
-----END PUBLIC KEY-----

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

openssl rsa -in test.pub -pubin -outform DER -out test_der.pub

The meaning of options:

  • -in test.pub - 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 test_der.pub - 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 test_der.pub -pubin -out test.pub

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.