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 $CRYPTOFILEPrompts 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 $FILEBetter 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-gcmprovides authenticated encryption (integrity + confidentiality).-pbkdf2 -iter 600000makes brute-force passphrase guessing expensive.-base64(-a) wraps the output in Base64 for text-safe transport.
Common pitfalls
- Legacy
openssl encdefaults are weak. OpenSSL 1.1.0 and earlier default to MD5-based key derivation. Always specify-pbkdf2on 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.
Related
- openssl-rsa-passphrase — managing the RSA keys that can protect symmetric session keys
- gpg-key-with-ssh-support — GPG is usually a better choice for long-term file encryption