Jump to content

Encrypt & Decrypt Files from the Command Line with OpenSSL


Recommended Posts

Posted

<div class="KonaBody"><p><img src="http://osxdaily.com/wp-content/uploads/2012/01/encrypt-file-openssl.jpg" alt="Encrypt and Decrypt files with OpenSSL" title="encrypt-file-openssl" width="620" height="246" class="aligncenter size-full wp-image-26580" /></p>

<p>Need to quickly encrypt a file from the command line? With OpenSSL, you can encrypt and decrypt files very easily. </p>

<p>For the purpose of this walkthrough, we’ll use des3 encryption, which in simple terms means a complex encryption algorithm is applied three times to each data block, making it difficult to crack through brute force methods. While we’re focusing on Mac OS X here, these commands will work anywhere that OpenSSL is installed, including older versions of OS X and Linux. </p>

<h2 style="font-size:1.3em;">Encrypting Files with OpenSSL</h2>

<p>The syntax of openssl is basic:</p>

<p><code>openssl [encryption type] -in [file to encrypt]</code></p>

<p>As mentioned before, we’ll use des3 for the encryption, and we’ll be using a text file as the input. We’re also going to specify a different output file to prevent any errors. Here is what the command would look like:</p>

<p><code>openssl des3 -in file.txt -out encrypted.txt</code></p>

<p>You will be asked to set and confirm a password before the encryption is complete, do not lose this password or you will lose access to the file.</p>

<p><em>Sidenote</em>: You can also just use an input file with -in filename, but that may cause issues. To prevent any unexpected problems, do not specify the same file as the input and output. This means the original file will stick around either before or after encryption, and you will want to deal with that file individually, preferably through a secure delete method. </p>

<h2 style="font-size:1.3em;">Decrypting Files with OpenSSL</h2>

<p><code>openssl des3 -d -in encrypted.txt -out normal.txt</code></p>

<p>The previously set password will be required to decrypt the file. </p>

<p>Other than switching the placement of the input and output, where again the original file stays put, the main difference here is the -d flag which tells openssl to decrypt the file.</p>

<p>Naturally, you’re probably wondering what happens if you try to open an file that has been encrypted with OpenSSL without entering the password? You’ll probably get an error message, but if you <a href="http://osxdaily.com/2007/05/22/ask-os-x-daily-how-do-i-force-open-a-file/">force open</a> the file with something like TextEdit, you’ll see the text “Salted” followed by a bunch of gibberish like so:</p>

<p><img src="http://osxdaily.com/wp-content/uploads/2012/01/encrypted-file.jpg" alt="Encrypted file" title="encrypted-file" width="565" height="120" class="aligncenter size-full wp-image-26573" /></p>

<p>The file will remain unreadable until it has been decrypted through openssl again. </p>

<p>For more about file security, don’t miss some of our other posts, including <a href="http://osxdaily.com/2011/01/21/password-protect-mac/">password protecting a Mac</a>, <a href="http://osxdaily.com/2012/01/25/password-protect-external-drive-mac-encrypted-partition/">encrypting partitions</a>, <a href="http://osxdaily.com/2012/01/07/set-zip-password-mac-os-x/">zip archives</a>, <a href="http://osxdaily.com/2012/01/11/password-protect-files-folders-in-mac-os-x/">files and folders in disk images</a>, and even <a href="http://osxdaily.com/2011/04/21/paranoid-about-iphone-ipad-location-tracking-encrypt-your-ios-backups/">encrypting iOS backups</a> to keep sensitive data from an iPhone and iPad secure. </p>

</div>

<p><a href="http://feedads.g.doubleclick.net/~a/JxjtNZ8OG_6cXHWuTGnluN4EynU/0/da"><img src="http://feedads.g.doubleclick.net/~a/JxjtNZ8OG_6cXHWuTGnluN4EynU/0/di" border="0" ismap="true"></img></a><br/>

<a href="http://feedads.g.doubleclick.net/~a/JxjtNZ8OG_6cXHWuTGnluN4EynU/1/da"><img src="http://feedads.g.doubleclick.net/~a/JxjtNZ8OG_6cXHWuTGnluN4EynU/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">

<a href="http://feeds.feedburner.com/~ff/osxdaily?a=4n9eu3TK8Sw:jzvmpZtTakU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/osxdaily?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/osxdaily?a=4n9eu3TK8Sw:jzvmpZtTakU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/osxdaily?i=4n9eu3TK8Sw:jzvmpZtTakU:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/osxdaily?a=4n9eu3TK8Sw:jzvmpZtTakU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/osxdaily?i=4n9eu3TK8Sw:jzvmpZtTakU:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/osxdaily?a=4n9eu3TK8Sw:jzvmpZtTakU:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/osxdaily?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/osxdaily?a=4n9eu3TK8Sw:jzvmpZtTakU:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/osxdaily?d=qj6IDK7rITs" border="0"></img></a>

</div><img src="http://feeds.feedburner.com/~r/osxdaily/~4/4n9eu3TK8Sw" height="1" width="1"/>

 

View the full article

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic


×
×
  • Create New...