Encrypting and decrypting files and directories securely is essential for protecting sensitive data. OpenSSL, a powerful open-source tool, provides robust encryption and decryption capabilities that can handle files of any size, from small text files to large binaries, and even entire directories. This tutorial will walk you through the process of encrypting and decrypting files and directories using OpenSSL on Linux systems such as Redhat, Ubuntu, Debian, CentOS, and Fedora.
In this tutorial you will learn:
How to create a sample file or directory
How to generate an RSA public and private key pair
How to encrypt a file or directory using OpenSSL
How to decrypt the encrypted file or directory back to its original state
Software Requirements and Linux Command Line Conventions
Category
Requirements, Conventions, or Software Version Used
System
Linux-based system (e.g., Redhat, Ubuntu, Debian, CentOS, Fedora)
Software
OpenSSL
Other
None
Conventions
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux commands to be executed as a regular non-privileged user
Creating a Sample File or Directory and Generating RSA Keys
To start, you will need a file or directory that you want to encrypt. For demonstration purposes, let’s create a sample file or directory and generate an RSA key pair.
Create a Sample File or Directory: You can create a file or a directory depending on what you want to encrypt. Here are the commands for both:For a file:
$ fallocate -l 100M sample_file.img
This command creates a file named sample_file.img of size 100MB.
This command creates a 2048-bit RSA key pair. The private key is stored in private-key.pem, and the public key is stored in public-key.pem. During this process, you will be prompted for certificate details, but you can skip these by pressing ENTER.
$ ls -l *.pem
Ensure that the private key is kept secure, as losing it will prevent you from decrypting your files or directories.
This command encrypts sample_directory.tar.gz and stores the encrypted output as sample_directory.tar.gz.enc.
Verify the Encryption: To ensure the encryption process was successful, generate the MD5 checksum of both the original and encrypted files or directories for comparison.For a file:
$ md5sum sample_file.img*
For a directory:
$ md5sum sample_directory.tar.gz*
The checksums should be different, confirming that the file or directory has been encrypted.
This command restores the original sample_directory.
Verify the Decryption: To verify that the decryption was successful, generate the MD5 checksum of the decrypted file or directory and compare it with the original file or directory’s checksum.For a file:
By following the steps outlined in this tutorial, you can easily encrypt and decrypt files and directories using OpenSSL on Linux. Whether you are dealing with small files, large files, or entire directories, OpenSSL provides a reliable and efficient method for securing your data. Always remember to keep your private key secure, as it is essential for decrypting your files or directories.