brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: openssl enc needs -pbkdf2 when you encrypt a file with a password

Openssl
~/tips/tip-openssl-enc-pbkdf2

openssl enc -aes-256-cbc -pbkdf2 -salt -in notes.txt -out notes.txt.enc

Encrypt and decrypt a single file with a password the safer way openssl enc offers.

openssl enc can encrypt a file with a password, but its old defaults are weak. Adding -pbkdf2 fixes the part that matters most.

Encrypt and decrypt like this; OpenSSL asks for the password:

openssl enc -aes-256-cbc -pbkdf2 -salt -in notes.txt -out notes.txt.enc
openssl enc -d -aes-256-cbc -pbkdf2 -in notes.txt.enc -out notes.txt

Without -pbkdf2, the key is made from the password with a single round of a fast hash, so guessing passwords is cheap. OpenSSL 3 even warns about it with deprecated key derivation used. With -pbkdf2, it uses PBKDF2 with 10000 rounds by default, and you can raise that with -iter. Use the same options to decrypt, or you get bad decrypt.

Add -a if you want base64 text output that survives copying into an email.

I would still reach for something else for real use. enc has no integrity check: it does not support GCM and refuses it with AEAD ciphers not supported, so a changed file can decrypt to garbage without a clear error. For files you care about, gpg --symmetric or age are better designed. enc is fine for a quick one off, as long as both sides have OpenSSL 1.1.1 or newer for -pbkdf2.

Further reading: OpenSSL: openssl-enc

More Openssl tips openssl s_client -connect 203.0.113.10:443 -servername example.com </dev/null | openssl x509 -noout -subject openssl x509 -in cert.pem -days 365 -signkey key.pem -out cert-new.pem printf %s hello | openssl dgst -sha256 -hmac secret openssl pkcs12 -legacy -in old.pfx -nokeys echo 'aGVsbG8gd29ybGQ=' | openssl base64 -d All Openssl tips (34) All topics

Link