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

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 🙂

🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @