Convert the key between different format
Encrypt a Private Key
This takes an unencrypted private key (unencrypted.key) and outputs an encrypted version of it (encrypted.key):
openssl rsa -des3 \
-in unencrypted.key \
-out encrypted.key
Enter your desired pass phrase, to encrypt the private key with.
Decrypt a Private Key
This takes an encrypted private key (encrypted.key) and outputs a decrypted version of it (decrypted.key):
openssl rsa \
-in encrypted.key \
-out decrypted.key
Enter the pass phrase for the encrypted key when prompted.
Convert Certificate Formats
All of the certificates that we have been working with have been X.509 certificates that are ASCII PEM encoded. There are a variety of other certificate encoding and container types; some applications prefer certain formats over others. Also, many of these formats can contain multiple items, such as a private key, certificate, and CA certificate, in a single file.
OpenSSL can be used to convert certificates to and from a large variety of these formats. This section will cover a some of the possible conversions.
Convert PEM to DER
Use this command if you want to convert a PEM-encoded certificate (domain.crt) to a DER-encoded certificate (domain.der), a binary format:
openssl x509 \
-in domain.crt \
-outform der -out domain.der
The DER format is typically used with Java.
Convert DER to PEM
Use this command if you want to convert a DER-encoded certificate (domain.der) to a PEM-encoded certificate (domain.crt):
openssl x509 \
-inform der -in domain.der \
-out domain.crt
Convert PEM to PKCS7
Use this command if you want to add PEM certificates (domain.crt and ca-chain.crt) to a PKCS7 file (domain.p7b):
openssl crl2pkcs7 -nocrl \
-certfile domain.crt \
-certfile ca-chain.crt \
-out domain.p7b
Note that you can use one or more -certfile options to specify which certificates to add to the PKCS7 file.
PKCS7 files, also known as P7B, are typically used in Java Keystores and Microsoft IIS (Windows). They are ASCII files which can contain certificates and CA certificates.
Convert PKCS7 to PEM
Use this command if you want to convert a PKCS7 file (domain.p7b) to a PEM file:
openssl pkcs7 \
-in domain.p7b \
-print_certs -out domain.crt
Note that if your PKCS7 file has multiple items in it (e.g. a certificate and a CA intermediate certificate), the PEM file that is created will contain all of the items in it.
Convert PEM to PKCS12
Use this command if you want to take a private key (domain.key) and a certificate (domain.crt), and combine them into a PKCS12 file (domain.pfx):
openssl pkcs12 \
-inkey domain.key \
-in domain.crt \
-export -out domain.pfx
You will be prompted for export passwords, which you may leave blank. Note that you may add a chain of certificates to the PKCS12 file by concatenating the certificates together in a single PEM file (domain.crt) in this case.
PKCS12 files, also known as PFX files, are typically used for importing and exporting certificate chains in Micrsoft IIS (Windows).
Convert PKCS12 to PEM
Use this command if you want to convert a PKCS12 file (domain.pfx) and convert it to PEM format (domain.combined.crt):
openssl pkcs12 \
-in domain.pfx \
-nodes -out domain.combined.crt
Note that if your PKCS12 file has multiple items in it (e.g. a certificate and private key), the PEM file that is created will contain all of the items in it.
Inspect the SSL cert
openssl.exe s_client -connect your.url.com:443 -showcerts | openssl x509 -text