{"id":2679,"date":"2016-11-05T10:41:41","date_gmt":"2016-11-05T10:41:41","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2679"},"modified":"2016-11-05T13:34:30","modified_gmt":"2016-11-05T13:34:30","slug":"encrypting-files-with-openssl","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/security\/encrypting-files-with-openssl","title":{"rendered":"Encrypting Files with openssl"},"content":{"rendered":"<h4>\nGood resources<br \/>\n<\/h4>\n<p>\n<a href=\"https:\/\/linuxconfig.org\/using-openssl-to-encrypt-messages-and-files-on-linux\">https:\/\/linuxconfig.org\/using-openssl-to-encrypt-messages-and-files-on-linux<\/a>\n<\/p>\n<h4>\nPublic&nbsp;key encrypt \/ private key decrypt &#8211; RSA (small files)<br \/>\n<\/h4>\n<p>\nNote RSA (rsault) is only suitable for very small files and is typically used to encrypt a randomly chosen private key that a larger file gets encrypted with. &nbsp;The reason it won&#39;t allow use with large files (say over around 512 bytes) is performance.\n<\/p>\n<h5>\nGenerating&nbsp;a key pair<br \/>\n<\/h5>\n<p>\nGenerate private key\n<\/p>\n<pre>\r\n<code>\r\nopenssl genrsa -out private_key.pem 1024\r\n<\/code><\/pre>\n<p>\nThen use it to generate the public key\n<\/p>\n<pre>\r\n<code>\r\nopenssl rsa -in private_key.pem -out public_key.pem -outform PEM -pubout\r\n<\/code><\/pre>\n<h5>\nEncrypt file<br \/>\n<\/h5>\n<p>\nThis will encrypt using RSA and your 1024 bit key.\n<\/p>\n<pre>\r\n<code>\r\nopenssl rsautl -encrypt -inkey public_key.pem -pubin -in encrypt.txt -out encrypt.dat\r\n<\/code><\/pre>\n<h5>\nDecrypt File<br \/>\n<\/h5>\n<pre>\r\n<code>\r\nopenssl rsautl -decrypt -inkey private_key.pem -in encrypt.dat -out new_encrypt.txt\r\n<\/code><\/pre>\n<h4>\nPublic&nbsp;key encrypt \/ private key decrypt &#8211; SMIME AES (Large files)<br \/>\n<\/h4>\n<p>\n(Good up to around&nbsp;500MB, dependant&nbsp;on platform and resources)\n<\/p>\n<h5>\nGenerating&nbsp;a key pair<br \/>\n<\/h5>\n<pre>\r\n<code>\r\nopenssl req -x509 -nodes -days 100000 -newkey rsa:2048  -keyout privatekey.pem  -out publickey.pem  -subj &#39;\/&#39;\r\n<\/code><\/pre>\n<h5>\nEncrypt file<br \/>\n<\/h5>\n<p>\nThis will encrypt using RSA and your 1024 bit key.\n<\/p>\n<pre>\r\n<code>\r\nopenssl  smime  -encrypt -aes256  -in  my_large_file.bin  -binary  -outform DEM  -out  my_large_file_encrypted.bin  publickey.pem\r\n<\/code><\/pre>\n<h5>\nDecrypt File<br \/>\n<\/h5>\n<pre>\r\n<code>\r\nopenssl  smime -decrypt  -in  my_large_file.bin  -binary -inform DEM -inkey privatekey.pem  -out  my_large_file.bin \r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Good resources https:\/\/linuxconfig.org\/using-openssl-to-encrypt-messages-and-files-on-linux Public&nbsp;key encrypt \/ private key decrypt &#8211; RSA (small files) Note RSA (rsault) is only suitable for very small files and is typically used to encrypt a randomly chosen private key that a larger file gets encrypted with. &nbsp;The reason it won&#39;t allow use with large files (say over around 512 bytes) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[134],"tags":[],"class_list":["post-2679","post","type-post","status-publish","format-standard","hentry","category-security"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2679"}],"version-history":[{"count":8,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2679\/revisions"}],"predecessor-version":[{"id":2687,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2679\/revisions\/2687"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}