Make a Simple Encrypt/Decrypt App

In this article we will be focusing on building a simple encryption and decryption app, using bash shell scripting and OpenSSL, and it will be a CLI app.

However, first it is necessary to create a good understanding about the basic concepts of encryption and decryption.

Encryption is basically the art of attaining confidentiality by converting a message into gibberish and can only be converted into a sensible message by the authorized personnel. It goes without saying this was an ancient need even in our history that is ravaged with war. During wartime it was a basic need to relay information between each others while not outsiders not knowing. And one of the very first people to come around this problem was our very own Julias Caesar. He created the Caesar cipher which is the most basic type of encryption. It basically shifts the alphabet by a certain number, The only people that knows this number are Caesar and the one he wants to communicate. By unshifting the gibberish words it is possible to uncover the hidden message.




                                                       

Modern Encryption

However the modern encryption algorithms are not that simple , the caesars ciper could easily be broken by guessing the shift key and applying , this mechanism is what known as "brute force" , it is the act of guessing all the possible combinations. In modern encryption the algorithms are very sophisticated and complex even a super computer would millions of years to crack it. There hardware based encryption algorithms like 3DES and Software based algorithms and some are combination of both like more modern AES algorithm. The keys used in these algorithms are quite big as well, the recommended size for the key is 2048 bit. There are 2 types of encryption algorithms as well.
          1. Symmetric Encryption
          2. Asymmetric Encryption
Symmetric Encryption - In these algorithm the encryption and the decryption is done through one key, hence the algorithm is less complex and the key space is smaller,

Asymmetric Encryption - In this algorithm instead of one key two keys are used, if you encrypt using one key the decryption process can only be done using the other key. And one key is always public , it is known as the public key and the other key is private key, it is as the name suggests unique and private. Although the public keys are public , sharing of these keys should be done in a organized manner, This is known as Public Key Infrastructure which is a whole another topic that will be discussed in a later post.

How the Encrypt/Decrypt App works

This is a very basic app it could be downloaded to your machine through my github repository:
  https://github.com/HashKushayne/encrypApp

Step 01: Make Public and Private Keys

This Application use RSA encryption algorithm which is a Asymmetric algorithm hence Public and Private keys needs to be made. We are going to use OpenSSL since that's what the application is made of.
 We first make the private key as follows.Then using it we make the private key.  


     

it will be visible as ".pem" files as given below.

Step 02: Organize the necessary files in a folder

Put the generated public.pem and private.pem files in a folder with the shell scripts ( files that you downloaded ) as follows. In this case I put them in the folder "encryptApp".

Step 03: Put the file that is needed to be encrypted on the following folder

For the folder that was made earlier, put the file that needed to be encrypted or decrypted in this case the text file called "Text.txt".

 and it has a plain text as shown above.

Step 04: Run the App

now run the App giving the command "./encryptApp.sh". make sure you are in the folder when you run the app. And also make sure the execution permission is given.

Step 05: Select the process

enter "e" to encrypt and "d" to decrypt. In this case "e" to encrypt Text.txt file


Step 06: Give the file name

enter the name of the file that is subjected to the process, In this case Text.txt needs to be encrypted.


And it's Done!!!




Code explanation: encryptApp.sh


In this first the welcoming messages are echoed  Then user inputs are taken in the variable $answer, and according to the process I have named the shell scrips, e.sh for the encryption and d.sh for the decryption. so the <user input>.sh is taken and executed in the command.


Code explanation: e.sh

First welcome message is echoed then the user input is taken for the name of the file to be encrypted in the variable $efile. Then OpenSSL command is given for the encryption with public key you just made and $efile as the input and $efile.enc as the output.

Since the both ciper text and the plain text are available the file taken in $efile will be deleted and $efile.enc will be rename to $efile.


Code explanation: d.sh

Same process as the e.sh but the decryption process only happens if the private key is available hence it given as an input. File removing and renaming process is similar to that of





Links

Github - https://github.com/HashKushayne/encrypApp
































Comments

Popular Posts