How to encrypt and decrypt files by openssl?

When we have sensitive data on server side that need to backup, by default, we would consider using a strong way to encrypt files. How can we do that in Linux command line?

Now we’ll show a typical example here,

Encrypt Files in Linux

To encrypt the content of a specified directory test :

# tar -czf - test | openssl enc -e -aes256 -out test-secured.tar.gz

This command will promote you to type in a password for encryption. Details of its parameters of the command is as follows,

enc – openssl command to encode with ciphers
-e – a enc command option to encrypt the input file, which in this case is the output of the tar command
-aes256 – the encryption cipher
-out – enc option used to specify the name of the out filename, test-secured.tar.gz

Decrypt Files in Linux

To decrypt a tar archive, use the following command.

# openssl enc -d -aes256 -in test-secured.tar.gz | tar xz -C test

Details of the above command:

-d – used to decrypt the files
-C – extract in subdirectory named test
Share Button

Leave a comment

Your email address will not be published. Required fields are marked *