File Password Protection

  • Post author:
  • Post category:CLI

How do I password protect / encrypt a file within Linux using OpenSSL ?

The file we will encrypt will be the file secretfile.txt.As you can see it is just a plain text file.

Encrypt File

Use the openssl comand to encrypt your file and then test the new file is fully encrypted.

$ openssl aes-256-cbc -salt -in secretfile.txt -out secretfile.txt.aes
enter aes-256-cbc encryption password:
Verifying – enter aes-256-cbc encryption password:

$ cat secretfile.txt.aes
binary data

Decrypt File

Decrypt the file and then confirm the decypted file is readable.

$ openssl aes-256-cbc -d -salt -in secretfile.txt.aes -out secretfile.txt
enter aes-256-cbc decryption password:

$ cat secretfile.txt
This is a secret file that we do not want anyone to read.

======================================

How do I password protect / encrypt a file within Linux using Mcrypt?
Encrypt File

Use the mcrypt comand to encrypt your file and then test the new file is fully encrypted.

$ mcrypt secretfile.txt

Output:
Enter the passphrase (maximum of 512 characters)
Please use a combination of upper and lower case letters and numbers.
Enter passphrase:
Enter passphrase:

A new file is created with the extension .nc

$ ls secretfile.txt.nc

Decrypt File

Decrypt the file and then confirm the decypted file is readable.

$ mcrypt -d secretfile.txt.nc

Output:

Enter passphrase:
File secretfile.txt.nc was decrypted.

======================================

How do I password protect / encrypt a file within Linux using GPG?
Encrypt File

Use the gpg comand to encrypt your file and then test the new file is fully encrypted.

$ gpg -c secretfile.txt

Output:
Enter passphrase:
Repeat passphrase:

This will create a secretfile.txt.gpg file.

Decrypt File

Decrypt the file and then confirm the decypted file is readable.

$ gpg secretfile.txt.gpg

Decrypt file and write output to file

$ gpg secretfile.txt.gpg -o somefile.txt

======================================

In all of the examples above we need to remove the original document once the new file is created.

======================================

Easiest method is to create a file using vim with the -x flag

vim -x

g33kadmin

I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....