Use OpenSSL to Encrypt and Decrypt Files

OpenSSL’s enc command provides symmetric encryption for files. It supports AES-256-CBC, AES-256-GCM, ChaCha20, and legacy ciphers like DES3.

Encrypt

openssl enc -aes-256-cbc -salt -in $FILE -out $CRYPTOFILE

Prompts for a passphrase and derives a key using PBKDF2 (or the older EVP_BytesToKey in pre-1.1.1 OpenSSL — use -pbkdf2 explicitly for modern, secure key derivation).

Decrypt

openssl enc -d -aes-256-cbc -in $CRYPTOFILE -out $FILE

Better modern practice

# Use PBKDF2 with a high iteration count and AES-256-GCM
openssl enc -aes-256-gcm -pbkdf2 -iter 600000 -salt -in $FILE -out $CRYPTOFILE
  • -aes-256-gcm provides authenticated encryption (integrity + confidentiality).
  • -pbkdf2 -iter 600000 makes brute-force passphrase guessing expensive.
  • -base64 (-a) wraps the output in Base64 for text-safe transport.

Common pitfalls

  • Legacy openssl enc defaults are weak. OpenSSL 1.1.0 and earlier default to MD5-based key derivation. Always specify -pbkdf2 on modern systems.
  • No integrity without AEAD. CBC mode alone is vulnerable to padding-oracle attacks. Prefer GCM or add an HMAC.
  • Passphrase-only encryption is only as strong as the passphrase. For high-value data, use a random keyfile or a hardware token.

Sources