Home » BASH » Encrypting/decrypting files from a script in Linux

Encrypting/decrypting files from a script in Linux

Last updated Sep 2, 2021

In this article, we are going to learn about OpenSSL. In this section, we are going encrypt and decrypt messages and files using OpenSSL.

Prerequisites

Besides having a terminal open, you need to have a basic knowledge of encoding and decoding schemes.

Encrypting/decrypting files

  • We are going to encrypt and decrypt simple messages. We will use the -base64 encoding scheme. First, we will encrypt a message. Run the following command in the terminal:
$ echo "Welcome to Bash Cookbook" | openssl enc -base64
  • To decrypt the message, run following command in the terminal:
$ echo " V2VsY29tZSB0byBCYXNoIENvb2tib29rCg==" | openssl enc -base64 -d
  • Now we are going to encrypt and decrypt files. First, we will encrypt a file. Run the following command in the terminal:
$ openssl enc -aes-256-cbc -in /etc/services -out enc_services.dat
  • Now we are going to decrypt a file. Run the following command in the terminal:
$ openssl enc -aes-256-cbc -d -in enc_services.dat > services.txt

How it works

OpenSSL is a tool used for encrypting and decrypting messages, files, and directories. In the preceding example, we used the -base64 scheme. The -d option is used for decryption.

To encrypt a file, we used the -in option, followed by the file that we want to encrypt, and the -out option instructed OpenSSL to store the file. It then stores the encrypted file by the specified name.

Related Posts

Creating a config file and using it in tandem with your scripts

In this article, we are going to create a config file and use it in our shell script.PrerequisitesBesides having a terminal open, you need basic knowledge of creating scripts and config files.Write scriptNow, we are going to create a script and config file. The...

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

Using Bash to monitor battery life and optimize it

In this article, we will learn about the TLP Linux tool. TLP is a command-line tool; it is used for power management and will optimize the battery life.PrerequisitesBesides having a Terminal open, you need to ensure that you have TLP installed on your system.How to do...

Creating a simple NAT and DMZ firewall using bash script

In this article, we will create a simple NAT firewall with DMZ using iptables.PrerequisitesBesides having a Terminal open, you need to ensure that iptables is installed in your machine.Write scriptWe will write a script to set up a DMZ using iptables. Create...

Follow Us

Our Communities

More on BASH

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!

0 Comments

0 Comments

Submit a Comment

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

nine − three =

Shares