How to Convert PFX to .CRT and .KEY Files Using OpenSSL: Step-by-Step Guide

Learn how to convert PFX files to .CRT and .KEY formats using OpenSSL in this easy step-by-step guide. Extract private keys for free with commands.6 min


9
7 comments, 9 points
pfx-to-crt-and-key-java-openssl-9Mood-beingcoders

TL;DR — This 7-minute guide will help you convert your .pfx file into .crt or .key file from the encrypted key using OpenSSL Commands for free. I kept 3 minutes as a buffer 😉. Using this trick, You can —

  • Convert PFX to CRT
  • Convert PFX to KEY
  • Extract private key from PFX
  • OpenSSL commands for SSL certificates
  • PFX to PEM conversion

Are you looking for extracting private key from PFX? — You are on right place.

Install OpenSSL:

OpenSSL is a robust, commercial-grade, and full-featured toolkit for the Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols. It is also a general-purpose cryptography library. For more information, you can visit the official website.

Let’s say, you have already got the .pfx certificate from the SSL providers/registrars like network solution, godaddy, bigrock etc., then you are good to follow up the below steps without any hurdles.

You need to follow up below commands in order to convert files to .crt/.key easily.

Prerequisites for Converting PFX Files

  1. OpenSSL package must be installed in your system.
  2. You must have .pfx file for your chosen domain name.
  3. Windows/Ubuntu/Linux system to utilize the OpenSSL package with crt

Image : pfx to crt and key | Image ©️ by Author Rakshit Shah

Step 1: Extract private key from PFX

  • Question: How do I extract a private key from a PFX file using OpenSSL?
  • Answer: You can extract the private key from a PFX file using the following OpenSSL command:
openssl pkcs12 -in [yourfilename.pfx] -nocerts -out [keyfilename-encrypted.key]

This command will extract the private key from the .pfx file. Now we need to type the import password of the .pfx file. This password is used to protect the keypair that was created for the .pfx file. After entering the import password OpenSSL requests to type another password twice. This new password is to protect the .key file. #SafetyFirst

theraxton@ubuntu:~/Downloads/SSL-certificate$ openssl pkcs12 -in samplefilename.pfx -nocerts -out samplefilenameencrypted.key 
Enter Import Password: 
Enter PEM pass phrase: 
Verifying — Enter PEM pass phrase: 
theraxton@ubuntu:~/Downloads/SSL-certificate$

Please note that, when you are going to enter the password, you can’t see against password, but they are typing in the back. Press enter once you entered your secure password.

Step 2: Convert PFX to CRT [Extract .crt file from the .pfx certificate]

openssl pkcs12 -in [yourfilename.pfx] -clcerts -nokeys -out [certificatename.crt]

After that, press enter and give the password for your certificate, and hit enter again, after all — your certificate will appear in the same directory.

theraxton@ubuntu:~/Downloads/SSL-certificate$ openssl pkcs12 -in samplefile.pfx -clcerts -nokeys -out samplefileencrypted.crt 
Enter Import Password:

Step 3: Convert PFX to KEY [Generate .KEY File Using OpenSSL]

[Extract the .key file from the encrypted private key from step 1]

openssl rsa -in [keyfilename-encrypted.key] -out [keyfilename-decrypted.key]

We need to enter the import password which we created in step 1. Now we have a certificate(.crt) and the two private keys ( encrypted and unencrypted).

theraxton@ubuntu:~/Downloads/SSL-certificate$ openssl rsa -in samplefilenameencrypted.key -out samplefilenameunencrypted.key 
Enter pass phrase for samplefilenameencrypted.key: 
writing RSA key

Now that you have successfully converted your PFX file into .crt and .key files, you can seamlessly use them in your Node / Angular / Java applications. Have any issues? Drop a comment below!


If you find joy and value in what I do, please consider supporting my work with a donation — however much you can afford, it means and helps more than you can imagine.

Buy Me A Coffee

adsense


Discover more from 9Mood

Subscribe to get the latest posts sent to your email.


Like it? Share with your friends!

9
7 comments, 9 points

What's Your Reaction?

Lol Lol
0
Lol
WTF WTF
0
WTF
Cute Cute
5
Cute
Love Love
5
Love
Vomit Vomit
0
Vomit
Cry Cry
0
Cry
Wow Wow
5
Wow
Fail Fail
0
Fail
Angry Angry
0
Angry
Rakshit Shah

Legend

Hey Moodies, Kem chho ? - Majama? (Yeah, You guessed Right! I am from Gujarat, India) 25, Computer Engineer, Foodie, Gamer, Coder and may be a Traveller . > If I can’t, who else will? < You can reach out me by “Rakshitshah94” on 9MOodQuoraMediumGithubInstagramsnapchattwitter, Even you can also google it to see me. I am everywhere, But I am not God. Feel free to text me.

7 Comments

Leave a Reply

  1. I tried like mentioned above: it worked for me!
    openssl pkcs12 -export -in certificate.cer -inkey privateKey.key -out certificate.pfx

    1. Hello Alpesh, thanks for sharing the answer with command.

      I have replaced certificate.cer with certificate.crt and it worked for me.

  2. Welldone, I have something for you to add in your answer. It might help people who are looking for this issue.

    If you have a self-signed certificate generated by makecert.exe on a Windows machine, you will get two files: cert.pvk and cert.cer. These can be converted to a pfx using pvk2pfx

    pvk2pfx is found in the same location as makecert (e.g. C:\Program Files (x86)\Windows Kits\10\bin\x86 or similar)

    pvk2pfx -pvk cert.pvk -spc cert.cer -pfx cert.pfx

    1. You need to use the makecert tool.

      Open a command prompt as admin and type the following:

      Command:
      makecert -sky exchange -r -n “CN=” -pe -a sha1 -len 2048 -ss My “.cer”

      Where = the name of your cert to create.

      Then you can open the Certificate Manager snap-in for the management console by typing certmgr.msc in the Start menu, click personal > certificates > and your cert should be available.

      Here is an article. check this out-

      https://azure.microsoft.com/documentation/articles/cloud-services-certs-create/

  3. # Solution for Windows that doesn’t require OpenSSL installed #

    I recently was trying to solve the same issue – and I only had a windows laptop with no openssl installed (and no enough admin rights to install it). Turns out windows has built-in utility called certutil that is capable of combining .crt and .key files into .pfx. Docs are here.

    You need to create a new folder and place you .crt and key files in it. Rename both files to have the same name (but different extension):

    {{sitename}}.crt
    {{siteName}}.key

    In case your key file is a regular txt – just change extension to .key.

    After that open cmd in that folder and run certutil -mergepfx [INPUTFILE] [OUTPUTFILE]

    Example:

    certificate file: mySite.crt

    key file: mySite.key

    certutil command: certutil -mergepfx mySite.crt mySite.pfx

    Note: you will be asked to provide password for newly created .pfx file – don’t forget to memorise/store it – as it will be required during certificate import on the target system.

  4. Awesome, this article worked for me…

    I created .pfx file from .key and .pem files.

    Like this >> openssl pkcs12 -inkey rootCA.key -in rootCA.pem -export -out rootCA.pfx

    That’s not the direct answer but still maybe it helps out someone else.

Choose A Format
Story
Formatted Text with Embeds and Visuals
List
The Classic Internet Listicles
Ranked List
Upvote or downvote to decide the best list item
Open List
Submit your own item and vote up for the best submission
Countdown
The Classic Internet Countdowns
Meme
Upload your own images to make custom memes
Poll
Voting to make decisions or determine opinions
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Personality quiz
Series of questions that intends to reveal something about the personality
is avocado good for breakfast? Sustainability Tips for Living Green Daily Photos Taken At Right Moment