Friday, May 25, 2012

How to encrypt and decrypt a file in Linux ?

There could be few ways to Encrypt and Decrypt a file in Linux. The one which I use is, gpg (GnuPG).
Below shown are the steps to encrypt and decrypt a file called "confidential.txt".

[root@hostxyz ashok]# echo 'newpassXYZ' > confidentail.txt
[root@hostxyz ashok]# cat confidentail.txt
newpassXYZ
[root@hostxyz ashok]# gpg -c confidential.txt
Enter passphrase:  <secret word>
Repeat passphrase: <secret word>
[root@hostxyz ashok]# ls -l confidentail.*
-rw-r--r-- 1 root root 11 May 22 22:01 confidential.txt
-rw-r--r-- 1 root root 66 May 22 22:01 confidential.txt.gpg
[root@hostxyz ashok]# cat confidential.txt.gpg
ê2Ãà pisÃu?î^ó5Ã\<dÃ
â[root@hostxyz ashok]# mv confidential.txt.gpg /tmp
[root@hostxyz ashok]# cd /tmp
[root@hostxyz tmp]# gpg -d confidential.txt.gpg
gpg: CAST5 encrypted data
Enter passphrase:  <secret word>
[root@hostxyz tmp]# cat confidential.txt
newpassXYZ
[root@hostxyz tmp]#

The config file for 'gpg' is /root/.gnupg/gpg.conf

2 comments:

  1. Hello,

    I did all steps which you have mentioned . But i am getting some warning message while decrypt the file also i could not find confidential.txt file at the present directory


    bash-3.2# gpg -d confidential.txt.gpg
    gpg: CAST5 encrypted data
    gpg: encrypted with 1 passphrase
    newpassXYZ
    gpg: WARNING: message was not integrity protected
    bash-3.2#

    bash-3.2# pwd
    /TEST
    bash-3.2# cat confidential.txt
    cat: confidential.txt: No such file or directory

    Please advice on this ( DEEPAK.L )

    ReplyDelete
  2. Hi Deepak,

    I guess you are doing copy and paste of my commands given in my post. Please type in or use TAB key to complete the command on your Shell prompt. When you copy and paste a command from a webpage, there will be one extra space at the end, which the OS consider as a character.

    [root@ashok tmp]# gpg -c confidential.txt <-- Copy and paste from my post
    gpg: can't open `confidential.txt': No such file or directory
    gpg: symmetric encryption of `confidential.txt' failed: file open error
    [root@ashok tmp]# gpg -c confidentail.txt <-- Typed in command
    Enter passphrase:

    -Ashok

    ReplyDelete