Skip to main content

πŸ” Encrypting and Decrypting Files Using GPG and OpenSSL πŸ”

This guide provides instructions on how to encrypt and decrypt files using both GPG (GNU Privacy Guard) and OpenSSL. Choose the tool that best fits your needs for secure file handling.

Using GPG (GNU Privacy Guard)

Encrypt a File with GPG

To encrypt a file using GPG, follow these steps. Ensure you have a GPG key pair set up before proceeding. Command:
  • --output encrypted_file.gpg: Specifies the name of the output encrypted file.
  • --encrypt: Indicates that you want to encrypt the file.
  • --recipient recipient@example.com: Specifies the recipient’s email associated with their GPG key.
  • plaintext_file.txt: The file you want to encrypt.
Example:

Decrypt a File with GPG

To decrypt a file encrypted with GPG, use the following command: Command:
  • --output decrypted_file.txt: Specifies the name of the output decrypted file.
  • --decrypt: Indicates that you want to decrypt the file.
  • encrypted_file.gpg: The file you want to decrypt.
Example:

Using OpenSSL

Encrypt a File with OpenSSL

To encrypt a file using OpenSSL, you can use the enc command with the desired encryption algorithm. The following example uses AES-256-CBC encryption. Command:
  • enc: Command to encode with encryption.
  • -aes-256-cbc: Specifies the encryption algorithm (AES in CBC mode with a 256-bit key).
  • -salt: Adds a salt to protect against dictionary attacks.
  • -in plaintext_file.txt: The input file to encrypt.
  • -out encrypted_file.enc: The output encrypted file.
Example:
You will be prompted to enter a password for encryption.

Decrypt a File with OpenSSL

To decrypt a file encrypted with OpenSSL, use the following command: Command:
  • -d: Specifies decryption.
  • -aes-256-cbc: Specifies the encryption algorithm used during encryption.
  • -in encrypted_file.enc: The input file to decrypt.
  • -out decrypted_file.txt: The output decrypted file.
Example:
You will be prompted to enter the password used for encryption.

Conclusion

Both GPG and OpenSSL offer robust solutions for encrypting and decrypting files:
  • GPG: Ideal for public-key encryption and secure exchanges.
  • OpenSSL: Suitable for symmetric key encryption and various security applications.
Select the tool that best meets your encryption needs and follow the provided commands to manage your files securely.