See posts by tags

Implementing GPG: Generating Keys and Encrypting Files

  • 1 min read
  • 17 Jul, 2024

Introduction

GnuPG, or GPG, is the open-source counterpart to the original Pretty Good Privacy (PGP) encryption system. It’s a robust tool for encrypting data and communications, and the de facto standard for this in Linux and other Unix-like systems.

Generating GPG Keys

To start using GPG, you need to create a key pair. The public key can be shared, while the private key remains with you.

  1. Open your terminal and enter:
    gpg --gen-key
  2. Select the type of key (RSA and RSA is recommended).
  3. Choose the key size (2048 bits for most users, 4096 bits for higher security).
  4. Set the key expiration time (2–3 years recommended).
  5. Enter your name, email address, and a comment.
  6. Create a passphrase to protect your private key.

After this, you will have a GPG key pair.

Encrypting and Decrypting Files with GPG

To encrypt a file using the public key, use:

gpg -e -u "Your Name" -r "Recipient Name" file.txt

To decrypt a file, use:

gpg -o file_decrypted.txt -d file.txt.gpg

Show All Public Keys with GPG

To view all public keys in your keyring, use:

gpg --list-keys

Export Public Keys with GPG

To export a public key, use:

gpg --export -a --output "file_output_name".asc "Email address or Name"

GPG is a powerful tool for protecting your confidential information. While it can be complex for beginners, its flexibility and reliability make it a valuable tool for encryption needs.