{"id":358,"date":"2006-02-13T11:08:21","date_gmt":"2006-02-13T10:08:21","guid":{"rendered":"http:\/\/wp.devco.net\/?p=358"},"modified":"2012-12-29T12:38:07","modified_gmt":"2012-12-29T11:38:07","slug":"public_-_private_key_encryption_using_openssl","status":"publish","type":"post","link":"https:\/\/www.devco.net\/archives\/2006\/02\/13\/public_-_private_key_encryption_using_openssl.php","title":{"rendered":"Public – Private key encryption using OpenSSL"},"content":{"rendered":"

Sometimes I need to encrypt some stuff but do not want to install PGP<\/a> or GPG<\/a>. I typically use OpenSSL<\/a> for this kind of thing and have written a simple frontend script<\/a> to achieve strong password based encryption using OpenSSL. Sometimes you need public \/ private key encryption though, below will show you how to do it using just OpenSSL.<\/p>\n

Public\/Private key encryption is a method used usually when you want to receive or send data to thirdparties. The system requires everyone to have 2 keys one that they keep secure – the private key – and one that they give to everyone – the public key. Data encrypted using the public key can only ever be unencrypted using the private key. This method of encryption that uses 2 keys is called asymmetric encryption.<\/p>\n

So by example if Person A want to send Person B data in a secure fashion she just have to encrypt it with Person B’s public key, only Person B can then open the file using her private key. There are other advantages to this kind of encryption. If I met you in person and gave you my public key, I can send you something electronically using my private key to encrypt it, if the public key you have can decrypt that data then you can trust that it was sent by me, it’s mathematical proof of identity. This is the basis for Digital Signatures.<\/p>\n

Using OpenSSL on the command line you’d first need to generate a public and private key, you should password protect this file using the -passout<\/i> argument, there are many different forms that this argument can take so consult the OpenSSL documentation about that.<\/p>\n

<\/p>\n

\r\n$ openssl genrsa -out private.pem 1024\r\n<\/pre>\n

<\/code><\/p>\n

This creates a key file called private.pem<\/i> that uses 1024 bits. This file actually have both the private and public keys, so you should extract the public one from this file:<\/p>\n

<\/p>\n

\r\n$ openssl rsa -in private.pem -out public.pem -outform PEM -pubout\r\n<\/pre>\n

<\/code><\/p>\n

You’ll now have public.pem<\/i> containing just your public key, you can freely share this with 3rd parties.
\nYou can test it all by just encrypting something yourself using your public key and then decrypting using your private key, first we need a bit of data to encrypt:<\/p>\n

<\/p>\n

\r\n$ echo 'too many secrets' > file.txt\r\n<\/pre>\n

<\/code><\/p>\n

You now have some data in file.txt<\/i>, lets encrypt it using OpenSSL and the public key:<\/p>\n

<\/p>\n

\r\n$ openssl rsautl -encrypt -inkey public.pem -pubin -in file.txt -out file.ssl\r\n<\/pre>\n

<\/code><\/p>\n

This creates an encrypted version of file.txt<\/i> calling it file.ssl<\/i>, if you look at this file it’s just binary junk, nothing very useful to anyone. Now you can unencrypt it using the private key:<\/p>\n

<\/p>\n

\r\n$ openssl rsautl -decrypt -inkey private.pem -in file.ssl -out decrypted.txt\r\n<\/pre>\n

<\/code><\/p>\n

You will now have an unencrypted file in decrypted.txt<\/i>:<\/p>\n

<\/p>\n

\r\n$ cat decrypted.txt
\r\ntoo many secrets\r\n<\/pre>\n

<\/code><\/p>\n

All of these examples use the RSA encryption method, some hard core mathematical information about it here<\/a>.<\/p>\n

There are a fair few limitations to this approach – it will only encrypt data up to the key size for example. And you really should never encrypt english plain text using a method like this. You’d use this to safely encrypt a random generated password and then aes encrypt the actual text you care about. Look in the comments for examples of that.<\/p>\n","protected":false},"excerpt":{"rendered":"

Sometimes I need to encrypt some stuff but do not want to install PGP or GPG. I typically use OpenSSL for this kind of thing and have written a simple frontend script to achieve strong password based encryption using OpenSSL. Sometimes you need public \/ private key encryption though, below will show you how to […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","footnotes":""},"categories":[7,5],"tags":[19,33],"_links":{"self":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/358"}],"collection":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/comments?post=358"}],"version-history":[{"count":4,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/358\/revisions"}],"predecessor-version":[{"id":1430,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/358\/revisions\/1430"}],"wp:attachment":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/media?parent=358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/categories?post=358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/tags?post=358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}