{"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 <\/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 <\/code><\/p>\n You’ll now have public.pem<\/i> containing just your public key, you can freely share this with 3rd parties. <\/code><\/p>\n You now have some data in file.txt<\/i>, lets encrypt it using OpenSSL and the public key:<\/p>\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 <\/code><\/p>\n You will now have an unencrypted file in decrypted.txt<\/i>:<\/p>\n<\/p>\n
\r\n$ openssl genrsa -out private.pem 1024\r\n<\/pre>\n
<\/p>\n
\r\n$ openssl rsa -in private.pem -out public.pem -outform PEM -pubout\r\n<\/pre>\n
\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
<\/p>\n
\r\n$ openssl rsautl -encrypt -inkey public.pem -pubin -in file.txt -out file.ssl\r\n<\/pre>\n
<\/p>\n
\r\n$ openssl rsautl -decrypt -inkey private.pem -in file.ssl -out decrypted.txt\r\n<\/pre>\n