Example : How To Encrypt / Decrypt A File In Linux

April 30, 2011 | By
| Reply More

If you are really concerned about the privacy and confidentiality of your files, encrypting files can help you. File encryption in Linux can be done in many ways, such as, using keys and software. GPG( Gnu Privacy Guard) by GNU is an encryption software provided free of charge. Here,we are trying to encrypt a file named “test”.

GPG Command Is Used to Encrypt.

The example given below can help you:

[root@mypc ]# gpg -c test

gpg: directory `/root/.gnupg' created

gpg: new configuration file `/root/.gnupg/gpg.conf' created

gpg: WARNING: options in `/root/.gnupg/gpg.conf' are not yet active during this run

gpg: keyring `/root/.gnupg/pubring.gpg' created

can't connect to `/root/.gnupg/S.gpg-agent': No such file or directory

gpg-agent[2043]: directory `/root/.gnupg/private-keys-v1.d' created

Please install pinentry-gui

gpg-agent[2043]: can't connect server: ec=4.16383

gpg-agent[2043]: can't connect to the PIN entry module: End of file

gpg-agent[2043]: command get_passphrase failed: No pinentry

gpg: problem with the agent: No pinentry

gpg: error creating passphrase: Operation cancelled

gpg: symmetric encryption of `test' failed: Operation cancelled

I got the errors above. So, I tried to reinstall GNUPG package as following:

[root@mypc ]# yum install gnupg

Loaded plugins: fastestmirror, refresh-packagekit, security

http://centos.aol.in/6.3/os/i386/repodata/repomd.xml: [Errno 14] Downloaded more than max size for http://mirror-status.centos.org: 113126 > 102400

Trying other mirror.

base                                                     | 3.7 kB     00:00

base/primary_db                                          | 3.5 MB     01:15

extras                                                   | 3.0 kB     00:00

extras/primary_db                                        | 6.4 kB     00:00

updates                                                  | 3.5 kB     00:00

updates/primary_db                                       | 1.1 MB     00:20

Setting up Install Process

Package gnupg2-2.0.14-4.el6.i686 already installed and latest version

Nothing to do

[root@mypc ]# gpg -c test

can't connect to `/root/.gnupg/S.gpg-agent': No such file or directory

Please install pinentry-gui

gpg-agent[2081]: can't connect server: ec=4.16383

gpg-agent[2081]: can't connect to the PIN entry module: End of file

gpg-agent[2081]: command get_passphrase failed: No pinentry

gpg: problem with the agent: No pinentry

gpg: error creating passphrase: Operation cancelled

gpg: symmetric encryption of `test' failed: Operation cancelled

For installing the missing package:

[root@mypc ]# yum install pinentry-gui

Loaded plugins: fastestmirror, refresh-packagekit, security

Determining fastest mirrors

* base: ftp.iitm.ac.in

* extras: ftp.iitm.ac.in

* updates: ftp.iitm.ac.in

base                                                     | 3.7 kB     00:00

extras                                                   | 3.0 kB     00:00

updates                                                  | 3.5 kB     00:00

Setting up Install Process

Resolving Dependencies

--> Running transaction check

---> Package pinentry-qt.i686 0:0.7.6-6.el6 will be installed

--> Finished Dependency Resolution

Dependencies Resolved

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

 Package              Arch          Version                 Repository     Size

Installing:

pinentry-qt          i686          0.7.6-6.el6             base           57 k

Transaction Summary

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

Install       1 Package(s)

Total download size: 57 k

Installed size: 132 k

Is this ok [y/N]: y

Downloading Packages:

pinentry-qt-0.7.6-6.el6.i686.rpm                         |  57 kB     00:02

warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY

Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

Importing GPG key 0xC105B9DE:

Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>

Package: centos-release-6-2.el6.centos.7.i686 (@base/$releasever)

From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

Is this ok [y/N]: y

Running rpm_check_debug

Running Transaction Test

Transaction Test Succeeded

Running Transaction

Installing : pinentry-qt-0.7.6-6.el6.i686                                 1/1

Installed:

pinentry-qt.i686 0:0.7.6-6.el6

Complete!

Now, we will try again:

[root@mypc ]# gpg -c testme

I entered my passphrase when prompted.

List files and you can see new file with .gpg extension.

[root@mypc ]# ls

encrypt3                          gnome-keyboard.desktop  test      testme

gnome-display-properties.desktop  test.gpg  testme.gpg

The file name 'testme.gpg' is the encrypted file.

[root@mypc ]# ls -al testme*

-rw-rw-r--. 1 centoslive centoslive  0 Nov 29 14:59 testme

-rw-r--r--. 1 root       root       45 Nov 29 14:59 testme.gpg

There are two file for testme:

[root@mypc ]# rm testme

rm: remove regular empty file `testme'? y

[root@mypc ]# ls -al testme*

-rw-r--r--. 1 root root 45 Nov 29 14:59 testme.gpg

To Decrypt the file using GPG:

[root@mypc ]# gpg  testme.gpg

Now, when I listed the files:

[root@mypc ]# ls -al testme*

-rw-r--r--. 1 root root  0 Nov 29 15:10 testme

-rw-r--r--. 1 root root 45 Nov 29 14:59 testme.gpg

[root@mypc ]#

The 'testme' file is created again after the decrypt.

Filed Under : LINUX HOWTO, SECURITY

Tagged With : ,

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.