HomeLinuxEncrypt and Decrypt Information with a Key on Linux

Encrypt and Decrypt Information with a Key on Linux


In computing, encryption is a well-liked and most occasions the advisable strategy of hiding data in a secretive format. GnuPG is without doubt one of the helpful instruments for encrypting data (recordsdata) on Linux techniques.

GnuPG (also called GNU Privateness Guard or just GPG) is GNU’s software used to encrypt knowledge and create digital signatures that contribute to general data safety. It’s a full and free implementation of the OpenPGP Web customary that gives a complicated key administration resolution.

There are two variations of GPG out there:

  1. gpg – a standalone model that’s extra suited to servers and embedded platforms.
  2. gpg2 – a model supposed for desktops and requires a number of different modules to be put in.

In some in style Linux distributions akin to Debian, the gnupg2 bundle is a dummy transitional bundle that gives symlinks from gpg2 to gpg.

This information reveals how one can generate a GPG key pair, export and share public keys, encrypt a file, and share and decrypt a file utilizing GPG in Linux techniques.

It demonstrates data sharing between two events:

  1. Kili Aaron ([email protected]) whose command immediate is [email protected]
  2. Take a look at Admin ([email protected]) whose command immediate is [email protected]

The file shared between the 2 events is named secret.txt, which accommodates a extremely delicate password that the Take a look at Admin needs to share with consumer Kili Aaron.

You may view the contents of the secret.txt file that accommodates the password and different distant entry particulars utilizing the next cat command as proven. It exists on the Take a look at Admin’s server:

# cat secret.txt
View Secret File Contents
View Secret File Contents

Set up GnuPG (GNU Privateness Guard) on Linux

To put in the GnuPG bundle, run the suitable command in your Linux distribution as proven. Be aware that the gnupg bundle should be put in on the 2 techniques sharing knowledge.

$ sudo apt set up gnupg          [On Debian, Ubuntu and Mint]
$ sudo yum set up gnupg          [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
$ sudo emerge -a app-crypt/gnupg  [On Gentoo Linux]
$ sudo apk add gnupg              [On Alpine Linux]
$ sudo pacman -S gnupg            [On Arch Linux]
$ sudo zypper set up gnupg       [On OpenSUSE]    

Producing New GPG Key Pairs in Linux

To generate new key pairs (private and non-private), run the gpg command with the --full-generate-key flag on each techniques and observe the prompts to outline the sort of key, the important thing measurement, how lengthy the important thing ought to be legitimate, a consumer ID to determine your key, and a safe passphrase for the important thing as proven within the screenshot that follows.

$ gpg --full-generate-key
Generating GPG Keys
Producing GPG Keys

Listing GPG Key Pairs in Linux

To listing the general public GPG key you have got simply created along with different current keys, run the gpg command with the --list-public-keys flag. To carry out an extended itemizing, add the --keyid-format=lengthy flag.

$ gpg --list-public-keys
OR
$ gpg --list-public-keys --keyid-format=lengthy
List GPG Keys
Listing GPG Keys

To listing the key GPG key you have got simply created along with different current keys, run the gpg command with the --list-secret-keys flag. To carry out an extended itemizing, add the --keyid-format=lengthy flag.

$ gpg --list-secret-keys
OR
$ gpg --list-secret-keys --keyid-format=lengthy
List Secret GPG Keys
Listing Secret GPG Keys

Export Keys with GPG in Linux

As soon as the GPG key pairs have been generated on either side, the 2 events can export their public keys right into a file and share through e mail or different means.

--------- On Kili Aaron Server --------- 
$ gpg --list-public-keys
$ gpg --export -o aaronsec.key 15B4814FB0F21208FB5076E7A937C15009BAC996

--------- On Take a look at Admin Server ---------
# gpg --list-public-keys
# gpg --export -o tadminsec.key BC39679E5FF48D4A6AEF6F3437211F0B4D6D8A61
Export GPG Keys
Export GPG Keys

Import Keys with GPG in Linux

Subsequent, trade the general public keys both through e mail or safe different means akin to utilizing the scp command as proven:

$ scp aaronsec.key [email protected]:/root/
$ scp [email protected]:/root/tadminsec.key ./
Exchange GPG Keys
Change GPG Keys

Subsequent, import the general public key from the alternative finish into the native system public keyring by including the --import flag as proven.

# gpg --import aaronsec.key
# gpg --import tadminsec.key
Import GPG Keys
Import GPG Keys

To examine if the imported public key exists within the native system keyring, listing the out there public keys as proven.

# gpg --list-public-keys
Confirm Imported GPG Keys
Verify Imported GPG Keys

Encrypting Information Utilizing GPG in Linux

Now let’s take a look at how one can encrypt the key file utilizing gpg keys. For this part, we’ll run the instructions on the Take a look at Admin’s server.

To encrypt a plain textual content file utilizing the simply created GPG key pair, run the next command. The -e or --encrypt flag permits encryption and the -r or --recipient flag is used to specify the recipient ID and secret.txt is the plain textual content file to be encrypted.

The next command encrypts the file secret.txt utilizing the recipient [email protected]’s public key:

#gpg -e -r [email protected] secret.txt  
OR
# gpg --encrypt --recipient [email protected] secret.txt

If the earlier command run efficiently, a brand new file (the unique filename ending with .gpg extension) will likely be generated within the present listing:

$ ls secret.txt.gpg
Encrypt File Using GPG in Linux
Encrypt File Utilizing GPG in Linux

To retailer the encrypted data in a special file, use the -o or --output choice adopted by a filename. On this instance, the popular filename is node_configs:

# gpg -e -r [email protected] -o node_configs secret.txt
OR
# gpg --encrypt --recipient [email protected] --output node_configs secret.txt

Now share the encrypted file along with your companion through e mail or different safe means.

Decrypting Information Utilizing GPG in Linux

To decrypt a file encrypted utilizing gpg, add the -d or --decrypt flag and specify the encrypted filename. By default, the decrypted data will likely be displayed in customary output. You may retailer it in a file utilizing the -o flag as proven.

$ gpg -d -o secrets and techniques.txt secrets and techniques.txt.gpg
$ ls secrets and techniques.txt
Decrypt File Using GPG in Linux
Decrypt File Utilizing GPG in Linux

For extra data, see the gpg/gpg2 man web page as proven.

$ man gpg
OR
$ man gpg2

That’s it for the scope of this information. GPG is a generally used software for encrypting and decrypting data or recordsdata in Linux. When you have any feedback to share about this information, use the suggestions type beneath.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments