How to convert .Pem to .Crt and .Key

Better Stack Team
Updated on October 7, 2024

To convert a .pem file into separate .crt (certificate) and .key (private key) files, you can use the openssl command-line tool. Here's how to do it:

Step 1: Understand the .pem File

A .pem file may contain several elements, such as:

  • The private key.
  • The public certificate.
  • The certificate chain (optional).

Typically, the private key and public certificate are stored in the .pem file.

Step 2: Extract the Private Key

To extract the private key from the .pem file:

 
openssl pkey -in yourfile.pem -out private.key
  • in yourfile.pem: The input .pem file.
  • out private.key: The output file for the private key.

Step 3: Extract the Certificate

To extract the certificate from the .pem file:

 
openssl x509 -in yourfile.pem -out certificate.crt
  • in yourfile.pem: The input .pem file.
  • out certificate.crt: The output file for the certificate.

Summary of Commands

  • Extract private key:

     
    openssl pkey -in yourfile.pem -out private.key
    
  • Extract certificate:

     
    openssl x509 -in yourfile.pem -out certificate.crt
    

Example

If your .pem file is named combined.pem, you can use the following commands:

  1. Extract the private key:

     
    openssl pkey -in combined.pem -out private.key
    
  2. Extract the certificate:

     
    openssl x509 -in combined.pem -out certificate.crt
    

Explanation

  • private.key: This file contains the private key extracted from the .pem file.
  • certificate.crt: This file contains the public certificate extracted from the .pem file.

Now, you have your private key in private.key and your certificate in certificate.crt. These files can be used as needed, for instance, in web server configurations or other services requiring separate certificate and key files.

Got an article suggestion? Let us know
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github