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

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

In the realm of secure communication and cryptography, the format in which cryptographic keys are stored can vary. One common format is PEM, which is a Base64-encoded format often used for representing private keys. However, there are scenarios where a different format, such as DER, is preferred. DER is a binary encoding format commonly used in various cryptographic applications. This tutorial explains how to convert PEM-encoded private key to DER-encoded using OpenSSL.

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

-----BEGIN PRIVATE KEY-----
MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEA3+i8CaGg06cTZWgl
5S6ekQH4LBPI9sojxWdD6strvmuIG2Z9isZ1QPwK/+n6KgvzrHBK9f4dheeQ998R
dzsiVQIDAQABAkA3eyS0wj7mkvykYPUa371csv86dMCUHixU6qySjkspSGcvdqNQ
UuAfRJ97n8m8wkNSHhF2HIWH2lPdC/MsZX/VAiEA8QLAZXzsUiEIQ04enLZRq0cv
YkD8mLyXVpT56ZrdEDsCIQDt1bMedIH1kCcAvg4PHAugXw1NeJSvpOpUqOyPwmB+
rwIhAJDhq5EW4OWaT+JOAt8IRt4k49o34OFcdcmpsvZ4jy3jAiA7n+2N3wuNspv0
lbEUnKVViT7egzJTbnbIzqivyb1DRQIhAKbr3cYY4mk0HKFQg3oEhWi2stSPulYz
KNkxLcXmqXjr
-----END PRIVATE KEY-----

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

openssl rsa -in test.key -outform DER -out test_der.key

The meaning of options:

  • -in test.key - specifies the input file, which is the PEM-encoded private key.
  • -outform DER - specifies the output format as DER.
  • -out test_der.key - specifies the output file, where the DER-encoded private key will be saved.

If you ever need to convert the DER-encoded private key back to PEM-encoded, you can use the following command:

openssl rsa -in test_der.key -out test.key

This command adheres to a comparable structure, taking the DER-encoded private key as the input file and generating the PEM-encoded private key as the output file.

Leave a Comment

Cancel reply

Your email address will not be published.