How to Encrypt and Decrypt Files and Directories Using Tar and OpenSSL

When you have important sensitive data, then its crucial to have an extra layer of security to your files and directories, specially when you need to transmit the data with others over a network.

That’s the reason, I am looking for a utility to encrypt and decrypt certain files and directories in Linux, luckily I found a solution that tar with OpenSSL can do the trick, yes with the help of these two tools you can easily create and encrypt tar archive file without any hassle.

Don’t Miss: 7 Tools to Encrypt and Decrypt Files in Linux

In this article, we will see how to create and encrypt a tar or gz (gzip) archive file with OpenSSL:

Remember that the conventional form of using OpenSSL is:

# openssl command command-options arguments

Encrypt Files in Linux

To encrypt the contents of the current working directory (depending on the size of the files, this may take a while):

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

Explanation of the above command:

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

Decrypt Files in Linux

To decrypt a tar archive contents, use the following command.

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

Explanation of the above command:

  1. -d – used to decrypt the files
  2. -C – extract in subdirectory named test

The following image shows the encryption process and what happens when you try to:

  1. extract the contents of the tarball the traditional way
  2. use the wrong password, and
  3. when you enter the right password
Encrypt or Decrypt Tar Archive File in Linux
Encrypt or Decrypt Tar Archive File in Linux

When you are working on a local network or the Internet, you can always secure your vital documents or files that you share with others by encrypting them, this can help reduce the risk of exposing them to malicious attackers.

We looked at a simple technique of encrypting tarballs using OpenSSL, a openssl command line tool. You can refer to its man page for more information and useful commands.

As usual, for any additional thoughts or simple tips that you wish to share with us, use the feedback form below and in the upcoming tip, we shall look at a way of translating rwx permissions into octal form.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

5 thoughts on “How to Encrypt and Decrypt Files and Directories Using Tar and OpenSSL”

  1. I am bothered by the comment of Quiark who claims to know something about the security of cryptographic methods, but shows that he knows nothing. Zomegagon references articles implying that openssl is inherently insecure, vs gpg.

    However, neither openssl nor gpg are cryptographic methods in themselves. They are front ends that call crypto algorithms from open source crypto libraries, and the user selects the crypto of his choice and the crypto parameters to suite his needs.

    These libraries are the gold standards upon which most of the modern internet depends. Unless you believe that closed source, proprietary cryptography is superior to open source cryptography. That argument, (security by obscurity) has been made many times and lost.

  2. This is unsafe. Please don’t write about crypto if you don’t understand it, putting your readers at risk (that probably want to encrypt sensitive data, you know?)


Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.