Encrypting/decrypting files from a script in Linux

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.

0 Comments

Submit a Comment

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

Related Articles