OpenSSL: Symmetric en- and decryption of a file


Hi,

this post describes the en- and decryption of a file with a symmetric encryption algorithm.

Symmetric encryption means encryption and decryption is only possible with the same secret/password.

An example. Create a file and encrypt a file with a password as single secret.

michael@debdev ~ # echo "My Secret Data" > file.txt
michael@debdev ~ # openssl aes-256-cbc -e -in file.txt -out encypted_file.txt
enter aes-256-cbc encryption password:

Decrypt the file with the given password

michael@debdev ~ # openssl aes-256-cbc -d -in encypted_file.txt -out clear_text_file.txt

also possible:

michael@debdev ~ # openssl enc -in file.txt -out encypted_file.txt -e -aes256
enter aes-256-cbc decryption password:
michael@debdev ~ # openssl enc -in encypted_file.txt -out clear_text_file.txt -d -aes256
enter aes-256-cbc decryption password:

Its also possible to use a secret stored in a file respectily a key.
Create a secret

michael@debdev ~ # openssl rand 256 > myEncryptionKeyFile.key

Encrypt the file with my key

michael@debdev ~ # openssl enc -in file.txt -out encypted_file.txt -e -aes256 -k myEncryptionKeyFile.key

Decrypt with the given key

michael@debdev ~ # openssl enc -in encypted_file.txt -out clear_text_file.txt -d -aes256 -k myEncryptionKeyFile.key

Michael

Leave a Reply