Skip to content

Instantly share code, notes, and snippets.

Last active December 14, 2023 05:42
Show Gist options
  • Star 87 You must be signed in to star a gist
  • Fork 41 You must be signed in to fork a gist
  • Save stevenhaddox/1501893 to your computer and use it in GitHub Desktop.
Save stevenhaddox/1501893 to your computer and use it in GitHub Desktop.
Convert .crt & .key files into .pem file for HTTParty

Two ways to do it, but only worked for me so I'll put it first and the second for reference:

$ openssl pkcs12 -export -in hostname.crt -inkey hostname.key -out hostname.p12
$ openssl pkcs12 -in hostname.p12 -nodes -out hostname.pem

Other options for this method in comments below:

# Note, the -certfile root.crt appends all CA certs to the export, I've never needed these so it's optional for my personal steps
$ openssl pkcs12 -export -in hostname.crt -inkey hostname.key -certfile root.crt -out hostname.p12
# Note, I've always had my hostname.crt as part of my .pem, so I keep my certs but apparently you may not have to, hence the nocerts flag being an extra option in this sample
$ openssl pkcs12 -in hostname.p12 -nocerts -nodes -out hostname.pem

The second method hasn't ever worked for me on the servers I've setup, but here it is for reference:

$ openssl x509 -in hostname.crt -inform DER -out hostname.crt.pem -outform PEM
$ openssl rsa -in hostname.key -out hostname.key.pem -outform PEM

Then to create the .pem I usually use just concat the two together with the PEM formatted certificate first and the key second.

Copy link


Copy link

thank you so much... u save me..

Copy link

you have "hsot" name in your code. If you cut and paste this, make sure you correct the type or you're not going to get too far.

Copy link


Copy link

matthax commented Aug 22, 2018

Thanks, forgot how to convert private keys when setting up a mysql instance. Second method worked perfectly

Copy link

hichMEN commented Jan 15, 2019

the command generate an empty file, any idea please?

Copy link

You king. Thanks for the help!

Copy link

Wow, I'd completely forgotten about this snippet. I'm glad to see it's been helpful to someone besides myself.

Copy link

Thank you very much. The first way worked to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment