Gregg's MOTD

Tips & Tricks that I've Encountered Over the Years...

Encrypting Files using OpenSSL

August 22, 2023 — Gregg Szumowski

Let’s say we have a file that contains sensitive information and we want to encyrpt it. You can encrypt a file very easily using the openssl command:

$ cat secret.txt
This file contains some very secret stuff
$ openssl enc -e -aes-256-cbc -pbkdf2 -a -salt -in secret.txt -out secret.enc
enter aes-256-cbc encryption password: <enter-a-password>
Verifying - enter aes-256-cbc encryption password: <enter-a-password>
$ cat secret.enc
U2FsdGVkX19Rnz48WjLeljd19wvNOhQy+zzYwxCANezCTkqpGMl9zs4HdwdUzZjl
VQkUsCJ7b0rUpRi83UlcwA==

Now we decrypt it (output to a different file). Make sure you enter the same password that you input above:

$ openssl enc -d -aes-256-cbc -pbkdf2 -a -salt -in secret.enc -out unencrypted-secret.txt
enter aes-256-cbc decryption password: <enter-a-password>
$ cat unencrypted-secret.txt
This file contains some very secret stuff
$

Tags: cli, openssl, encryption, motd