Linux > How to encrypting a files for particular user (recipients aka your trusted friend)

last updated in Categories News

1) In order to encrypt a file for your friend or recipients you need to obtains his/hers GnuPG public key. It can be obtained why an email, keyserver or even usb pen device. Then import the friends public using following command:
$ gpg –import

2) Once key added to your GnuPG key ring, it is time to encrypt the file(s) using your private key and the recipient’s public key so that only your particular friend or recipients can decrypt it:
gpg -e -r Public_key_ID file

For example to decrypt, file myfin.rec.txt:
$ gpg -e -r A560159A myfin.rec.txt
$ gpg -e -r vivek myfin.rec.txt
$ gpg รขโ‚ฌโ€œa -e -r vivek myfin.rec.txt

gpg: 54CCC70D: There is no indication that this key really belongs to the owner
2048g/54CCC70D 2005-03-18 “Vivek G Gite (My keys for all friends) ”
Primary key fingerprint: 00BD 76AB A6EC 574B F3F7 4BC5 EAF2 7147 A560 159A
Subkey fingerprint: 8D5E 4B3E 17D9 F491 CA04 D4B7 1BF4 E16C 54CC C70D

It is NOT certain that the key belongs to the person named
in the user ID. If you *really* know what you are doing,
you may answer the next question with yes

Use this key anyway? yes

-e : Encrypt data.
-r : Encrypt for user id name. You can specify the user id (vivek) or Public_key_id
(A560159A). Note use gpg –list-public-key to obtain the Public_key_ids.
-a : Create ASCII armored output so that file can be send via an email

3) Use the email to send the file to your friend/ recipients so that only he/she can decrypt it.

4) When your file reached to desire user/friend/recipients s/he need to run simply gpg command on it:
$ gpg myfin.rec.txt.asc

gpg myfin.rec.txt.asc

You need a passphrase to unlock the secret key for
user: “Vivek G Gite (My keys for all friends) ”
2048-bit ELG-E key, ID 54CCC70D, created 2005-03-18 (main key ID A560159A)
Enter passphrase: MYPASS

gpg: encrypted with 2048-bit ELG-E key, ID 54CCC70D, created 2005-03-18
“Vivek G Gite (My keys for all friends) ”

5) If some other person steals your file and tries to decrypt it using gpg:
$ gpg myfin.rec.txt.asc
gpg: encrypted with ELG-E key, ID 54CCC70D
gpg: decryption failed: secret key not available

It should fail with above error message and that is the whole point to use the GnuPG ๐Ÿ™‚

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.