How to use the command 'gpg2' (with examples)

How to use the command 'gpg2' (with examples)

GNU Privacy Guard 2 (gpg2) is an encryption program that allows users to encrypt and decrypt files, as well as import and export keys for secure communication. This article provides examples of different use cases of the gpg2 command.

Use case 1: List imported keys

Code:

gpg2 --list-keys

Motivation: Listing imported keys allows users to view the keys that have been imported and are available for use in encryption or decryption processes.

Explanation: The --list-keys option is used to display a list of keys in the keyring. This includes both public and private keys.

Example output:

pub   rsa2048 2022-01-01 [SC]
      89ABCDEF01234567ABCDEF0123456789ABCDEF0
uid           [ unknown] John Doe <john.doe@example.com>

Use case 2: Encrypt a specified file for a specified recipient

Code:

gpg2 --encrypt --recipient alice@example.com path/to/doc.txt

Motivation: Encrypting a file for a specific recipient ensures that only the recipient with the corresponding private key can decrypt and access the contents of the file.

Explanation: The --encrypt option is used to encrypt the file specified by path/to/doc.txt. The --recipient option specifies the email address or key ID of the recipient for whom the file should be encrypted.

Example output: A new file named doc.txt.gpg is created, which contains the encrypted contents of doc.txt for the specified recipient.

Use case 3: Encrypt a specified file with only a passphrase

Code:

gpg2 --symmetric path/to/doc.txt

Motivation: Encrypting a file with only a passphrase provides an additional layer of security, as the file can only be decrypted using the same passphrase.

Explanation: The --symmetric option is used to encrypt the file specified by path/to/doc.txt using a symmetric encryption algorithm. This means that the same passphrase used for encryption will be required for decryption.

Example output: A new file named doc.txt.gpg is created, which contains the encrypted contents of doc.txt using the symmetric encryption algorithm.

Use case 4: Decrypt a specified file

Code:

gpg2 --decrypt path/to/doc.txt.gpg

Motivation: Decrypting a file allows users to access the contents of an encrypted file using the corresponding private key or passphrase.

Explanation: The --decrypt option is used to decrypt the file specified by path/to/doc.txt.gpg and write the result to stdout.

Example output: The decrypted contents of doc.txt.gpg are displayed in the terminal.

Use case 5: Import a public key

Code:

gpg2 --import path/to/public_key.gpg

Motivation: Importing a public key allows users to add the key to their keyring and use it for encryption or verification purposes.

Explanation: The --import option is used to import the public key contained in the file specified by path/to/public_key.gpg into the keyring.

Example output: The public key is successfully imported into the keyring.

Use case 6: Export the public key of a specified email address

Code:

gpg2 --export --armor alice@example.com

Motivation: Exporting the public key of a specific email address allows users to share their public key with others for encryption or verification purposes.

Explanation: The --export option is used to export the public key associated with the specified email address (alice@example.com in this case). The --armor option ensures that the exported key is in ASCII armor format for easy sharing.

Example output: The ASCII armored representation of the public key associated with alice@example.com is displayed in the terminal.

Use case 7: Export the private key with a specified email address

Code:

gpg2 --export-secret-keys --armor alice@example.com

Motivation: Exporting the private key associated with a specific email address allows users to securely back up their private key or transfer it to a different system.

Explanation: The --export-secret-keys option is used to export the private key associated with the specified email address (alice@example.com in this case). The --armor option ensures that the exported key is in ASCII armor format.

Example output: The ASCII armored representation of the private key associated with alice@example.com is displayed in the terminal.

Conclusion:

The gpg2 command provides a wide range of options for managing keys and encrypting/decrypting files. By following the examples provided in this article, users can effectively utilize the gpg2 command for secure communication and data protection.

Related Posts

How to use the command ego (with examples)

How to use the command ego (with examples)

The command “ego” is Funtoo’s official system personality management tool. It provides various functionalities such as synchronizing the Portage tree, updating the bootloader configuration, reading a Funtoo wiki page, printing the current profile, enabling/disabling mix-ins, and querying Funtoo bugs related to a specified package.

Read More
How to use the command 'direnv' (with examples)

How to use the command 'direnv' (with examples)

Direnv is a shell extension that allows you to load and unload environment variables depending on the current directory.

Read More
How to use the command "f3read" (with examples)

How to use the command "f3read" (with examples)

To validate a device by checking the files in a given directory, you can use the f3read command followed by the path to the mount point.

Read More