Linux
OS-Mgmt
File Encryption

🔐 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:

gpg --output encrypted_file.gpg --encrypt --recipient [email protected] plaintext_file.txt
  • --output encrypted_file.gpg: Specifies the name of the output encrypted file.
  • --encrypt: Indicates that you want to encrypt the file.
  • --recipient [email protected]: Specifies the recipient’s email associated with their GPG key.
  • plaintext_file.txt: The file you want to encrypt.

Example:

gpg --output secret.txt.gpg --encrypt --recipient [email protected] secret.txt

Decrypt a File with GPG

To decrypt a file encrypted with GPG, use the following command:

Command:

gpg --output decrypted_file.txt --decrypt encrypted_file.gpg
  • --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:

gpg --output secret.txt --decrypt secret.txt.gpg

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:

openssl enc -aes-256-cbc -salt -in plaintext_file.txt -out encrypted_file.enc
  • 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:

openssl enc -aes-256-cbc -salt -in secret.txt -out secret.txt.enc

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:

openssl enc -d -aes-256-cbc -in encrypted_file.enc -out decrypted_file.txt
  • -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:

openssl enc -d -aes-256-cbc -in secret.txt.enc -out secret.txt

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.


🧙 AI Wizard - Instant Page Insights

Click the button below to analyze this page.
Get an AI-generated summary and key insights in seconds.
Powered by Perplexity AI!