Encrypt Files with OpenSSL in Linux

Encrypt Files with OpenSSL in Linux

Do you need to send a sensitive or secret email through Gmail or another email provider? Or have you ever needed to keep sensitive company info on a personal computer while traveling? If so you should encrypt these files so that in case of your email being intercepted (emails are sent across the internet in plain text) or your computer being stolen, your sensitive info is protected. In this tutorial we explore encrypting files in Linux using OpenSSL password based encryption. OpenSSL encrypts a document using a password and then decrypts the file with the same password you set when encrypting the file. Let’s view it in action! 

Encrypt and Decrypt a File

The first thing we are going to do is create a .txt file and write something in it such as “this is my secret text file” then save the file and exit the editor. Once the file is created, navigate to the directory where you saved the file to in your terminal and enter: 

							
							
					openssl enc -aes-256-cbc -salt -in yourfile.txt -out yourfile.txt.enc
				
			

You will be prompted to enter a password which we will use in the future to decrypt the file, it will then ask you to confirm the password. Once you have encrypted the file, try viewing the encrypted file either with “cat” in the terminal or with a text editor. You should see a randomized string of text, characters, and numbers. Now let’s decrypt our encrypted file, in the terminal enter:

							
							
					openssl enc -d -aes-256-cbc -in yourfile.txt.enc -out yourfile.txt.dec
				
			

You will be prompted to enter the password we created when we encrypted the file, once the file is decrypted, view it eitehr with “cat” in the terminal or with a text editor, you should see the file decrypted just as when you first created it. That’s it, you have successfully encrypted and decrypted a file using OpenSSL.

Walter Miely is a tech entrepreneur and CEO of Phoenix Ignited Tech You can find him on Linkedin. This material is licensed under the CC BY 4.0 License LEGAL DISCLAIMER: The content provided here is provided AS IS, and part of, or the entirety of this content may be incorrect. Please read the entireLegal Disclaimer here.