AMP AMP

How to Encrypt/Decrypt and Password Protect Files in Linux

To Encrypt/Decrypt and Password Protect Files in Linux

Steps to encrypt or decrypt and password protect files in linux using 7 tools is discussed in this manual with examples.
The process of encoding the files such that only authorized users can access it is called as Encryption.

GnuPG

GnuPG ( GNU Privacy Guard ) is often called as GPG. It is a collection of cryptographic software which is written by GNU Project in C programming Language.

The gnupg package is default in Linux distributions, if it is not available then you can apt or yum it from repository.

[user1@linuxhelp:~$ sudo apt-get install gnupg
[sudo] password for user1: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
gnupg is already the newest version.
The following packages were automatically installed and are no longer required:
  app-install-data apt-xapian-index apturl-common avahi-utils cups-browsed
  cups-server-common gir1.2-gmenu-3.0 hplip-data laptop-detect libart-2.0-2


Text file (linuxhelp.txt) located at ~/Desktop/Linuxhelp/ is used for example

To check the content of the text file

[user1@linuxhelp:~$ cat ~/Desktop/linuxhelp/linuxhelp.txt 
welcome to linuxhelp
we provide best tutorial for linux lover

To encrypt linuxhelp.txt file using gpg

Run the gpc command with option -c to create a file linuxhelp.txt.gpg.

[user1@linuxhelp:~]$ gpg -c ~/Desktop/linuxhelp/linuxhelp.txt 
[user1@linuxhelp:~]$ ls -l ~/Desktop/linuxhelp/
total 16
-rw-rw-r-- 1 user1 user1  65 Mar 21 17:48 linuxhelp.txt
-rw-rw-r-- 1 user1 user1 106 Mar 21 17:52 linuxhelp.txt.gpg 

The encryption was done with CAST5 encryption algorithm automatically.

To see all the encryption algorithm present

Execute the following command to see all the encryption algorithm present.

[user1@linuxhelp:~]$ gpg --version
gpg (GnuPG) 1.4.16
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later < http://gnu.org/licenses/gpl.html> 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Home: ~/.gnupg
Supported algorithms:
Pubkey: RSA, RSA-E, RSA-S, ELG-E, DSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
        CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: MD5, SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2

To decrypt the encrypted file

The following command is used to decrypt the encrypted file. First we will remove the original file i.e., linuxhelp.txt and leave the encrypted file linuxhelp.txt.gpg untouched.

[user1@linuxhelp:~]$ rm ~/Desktop/linuxhelp/linuxhelp.txt
[user1@linuxhelp:~]$ gpg ~/Desktop/linuxhelp/linuxhelp.txt.gpg 
gpg: CAST5 encrypted data
gpg: encrypted with 1 passphrase
gpg: WARNING: message was not integrity protected

Provide the same password you provided at encryption to decrypt when prompted.

bcrypt

bcrypt is based on Blowfish cipher. It is a key derivation function which is not recommended since cipher algorithm can be attacked.

To install bcrypt

You can apt or yum the required package.

[user1@linuxhelp:~]$ sudo apt-get install bcrypt
Reading package lists... Done
Building dependency tree       
Reading state information... Done
bcrypt is already the newest version.
The following packages were automatically installed and are no longer required:
  app-install-data apt-xapian-index apturl-common avahi-utils cups-browsed
  cups-server-common gir1.2-gmenu-3.0 hplip-data laptop-detect libart-2.0-2
  libcupscgi1 libcupsmime1 libgutenprint2 libhpmud0 libqt4-designer

To Encrypt the file using bcrypt

[user1@linuxhelp:~]$ bcrypt ~/Desktop/linuxhelp/linuxhelp.txt
Encryption key:
Again:

To list the file

[user1@linuxhelp:~]$ ls -l ~/Desktop/linuxhelp/
total
-rw-r--r-- 1 user1 user1 134 Mar 21 18:05 linuxhelp.txt.bfe


The new file name linuxhelp.txt.bfe is created and original file linuxhelp.txt gets replaced.

To decrypt the file using bcrypt

[user1@linuxhelp:~]$ bcrypt ~/Desktop/linuxhelp/linuxhelp.txt.bfe 
Encryption key:

ccrypt

ccrypt is a replacement of UNIX crypt which is an utility for files and streams encryption and decryption. It uses Rijndael cipher.

To install ccrypt

You can apt or yum the required package.

[user1@linuxhelp:~]$ sudo apt-get install ccrypt
Reading package lists... Done
Building dependency tree       
Reading state information... Done
ccrypt is already the newest version.
The following packages were automatically installed and are no longer required:
  app-install-data apt-xapian-index apturl-common avahi-utils cups-browsed
  cups-server-common gir1.2-gmenu-3.0 hplip-data laptop-detect libart-2.0-2
  libcupscgi1 libcupsmime1 libgutenprint2 libhpmud0 libqt4-designer
Note: It uses ccencrypt to encrypt and ccdecrypt to decrypt. During the process encryption, the original file (linuxhelp.txt) is replaced by (linuxhelp.txt.cpt) and at the decryption the encrypted file (linuxhelp.txt.cpt) is replaced by original file (linuxhelp.txt).

To Encrypt a file

[user1@linuxhelp:~]$ ccencrypt ~/Desktop/linuxhelp/linuxhelp.txt
Enter encryption key: 
Enter encryption key: (repeat)  

To view the file

[user1@linuxhelp:~]$ cat ~/Desktop/linuxhelp/linuxhelp.txt.cpt 
9ï ¿ ½ zQï ¿ ½ ï ¿ ½ ï ¿ ½ ï ¿ ½ yï ¿ ½ Ù ?/ï ¿ ½ Wï ¿ ½ #ï ¿ ½ 5Jï ¿ ½ ï ¿ ½ ï ¿ ½ ï ¿ ½ ï ¿ ½ ï ¿ ½ ï ¿ ½ 0~ï ¿ ½ ï ¿ ½ ï ¿ ½ ï ¿ ½ ~ 
                 ï ¿ ½ ï ¿ ½ ï ¿ ½ Uvï ¿ ½ *Lï ¿ ½ #ï ¿ ½ ï ¿ ½ :Hï ¿ ½ ï ¿ ½ $%Zujï ¿ ½ ï ¿ ½ ï ¿ ½ -#fï ¿ ½ [ï ¿ ½ iï ¿ ½ ï ¿ ½ è ?¼ 

To Decrypt a file

[user1@linuxhelp:~]$ ccdecrypt ~/Desktop/linuxhelp/linuxhelp.txt.cpt 
Enter decryption key: 

To view the file

[user1@linuxhelp:~]$ cat ~/Desktop/linuxhelp/linuxhelp.txt
welcome to linuxhelp
we provide best tutorial for linux lover

Give the same password details

Zip

Zip is most famous archive format and we generally call archive files as zip files. It uses pkzip stream cipher algorithm.

To install zip

You can apt or yum the required package.

[user1@linuxhelp:~/Desktop/linuxhelp]$ sudo apt-get install zip
Reading package lists... Done
Building dependency tree       
Reading state information... Done

To Create a encrypted zip file

[user1@linuxhelp:~/Desktop/linuxhelp]$ zip --password mypassword linuxhelp.zip linuxhelp.txt linuxhelp1.txt 
  adding: linuxhelp.txt (deflated 9%)
  adding: linuxhelp1.txt (stored 0%)

mypassword is the password used to encrypt it.

[user1@linuxhelp:~/Desktop/linuxhelp]$ ls -l
total 20
-rw-rw-r-- 1 user1
-rw-rw-r-- 1 user1 user1  47 Mar 16 17:08 linuxhelp1.txt
-rw-r--r-- 1 user1 user1  65 Mar 21 18:10 linuxhelp.txt
-rw-rw-r-- 1 user1 user1 494 Mar 21 18:29 linuxhelp.zip

To decrypt the password protected zipped file using unzip

[user1@linuxhelp:~/Desktop/linuxhelp]$ unzip linuxhelp.zip 
Archive:  linuxhelp.zip
[linuxhelp.zip] linuxhelp.txt password: 
replace linuxhelp.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
  inflating: linuxhelp.txt           
 extracting: linuxhelp1.txt          

Openssl

Openssl is a cryptographic toolkit which can be used to encrypt message and files

To install openssl

[user1@linuxhelp:~/Desktop/linuxhelp]$ sudo apt-get install openssl
Reading package lists... Done
Building dependency tree       
Reading state information... Done
...
...

To encrypt a file using openssl encryption

[user1@linuxhelp:~/Desktop/linuxhelp]$ openssl enc -aes-256-cbc -in ~/Desktop/linuxhelp/linuxhelp.txt -out ~/Desktop/linuxhelp/linuxhelp.dat
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
Explanation of each option used
-in : full path of file to be encrypted.
-aes-256-cbc : the algorithm to be used.
enc : encryption
-out : full path where it will be decrypted.

To decrypt a file using openssl

[user1@linuxhelp:~/Desktop/linuxhelp]$ openssl enc -aes-256-cbc -in ~/Desktop/linuxhelp/linuxhelp.dat >  ~/Desktop/linuxhelp/linuxhelp.txt
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:

7-zip

7-zip is an open source written in C++. It is used to compress and uncompress the known archive file format.

To install

[user1@linuxhelp:~]$ sudo apt-get install p7zip-full
Reading package lists... Done
Building dependency tree       
Reading state information... Done

To compress files into zip using 7-zip and encrypt it

[user1@linuxhelp:~/Desktop/linuxhelp]$ 7za a -tzip -p -mem=AES256 linuhelp7zip.zip linuxhelp1.txt 

7-Zip (A) [64] 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18
p7zip Version 9.20 (locale=en_IN,Utf16=on,HugeFiles=on,1 CPU)
Scanning

Creating archive linuhelp7zip.zip

Enter password (will not be echoed) :
Verify password (will not be echoed) :
Compressing  linuxhelp1.txt      

Everything is Ok

To view the file

[user1@linuxhelp:~/Desktop/linuxhelp]$ ls -l
total 28
-rw-rw-r-- 1 user1 user1 223 Mar 21 18:49 linuhelp7zip.zip

To decompress encrypted zip file using 7-zip

[user1@linuxhelp:~/Desktop/linuxhelp]$ 7za e linuhelp7zip.zip /span> 

7-Zip (A) [64] 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18
p7zip Version 9.20 (locale=en_IN,Utf16=on,HugeFiles=on,1 CPU)

Processing archive: linuhelp7zip.zip

file linuxhelp1.txt
already exists. Overwrite with 
linuxhelp1.txt?
(Y)es / (N)o / (A)lways / (S)kip all / A(u)to rename all / (Q)uit? A
Extracting  linuxhelp1.txt
Enter password (will not be echoed) :
Everything is Ok

Size:       47
Compressed: 223

Nautilus Encryption Utility

Here are the steps to encrypt files in GUI using Nautilus encryption utility.

Encryption of file in GUI

1. Right click the file to encrypt.

2. Select format to zip and provide location to save. Provide password to encrypt as well.

3. Message? encrypted zip created successfully.


Decryption of file in GUI

1. Open the zip in GUI. View the LOCK-ICON next to file. It will prompt for password, Enter it.


2. Open the file for you

Tag : Decrypt
FAQ
Q
Why aren’t there any servers in my area?
A
Your computer’s firewall or a proxy server might be blocking communication over port 8080, which will limit the number of servers available for testing.
Q
is there any tool to save password
A
keepasswd
Q
I hear about that using VIM editor also we can set encrytion . But I don’t know how ? Pls help me
A
open a file with VIM and then press :X and give password
Q
is there any tool to crack the password in linux
A
in kali linux, the tools rarcrack and fcrackzip
Q
how to change password for a user
A
passwd username