Node.js encrypt-decrypt-it module and its usage

Here I am going to introduce to a useful Node.js module that is the encrypt-decrypt-it module. With this module, you can encrypt a data or string and using the same module, you can also decrypt the string.

Installation of encrypt-decrypt-it

From the Node.js command prompt, you have to be inside the project where you want to use the encrypt-decrypt-it module. There you can install this package from npm.

Below is the command to install the encrypt-decrypt-it from the npm:

>npm i encrypt-decrypt-it

That’s it. The package will be than install successfully.

Usage of the encrypt-decrypt-it module

Now its time to see how to use the module that we have just installed.

The module has only two methods available to use. One is the encryptIt() and the other one is the decryptIt() method.

Syntax for encryptIt():

Below is the syntax for the encryptIt() method:

encryptIt(string, password, cipher_algorithm)

Below is the brief description of each parameter of encryptIt() function:

  • string: Our original string that needs to be encrypted. This is a required parameter.
  • password: The password key to encrypt our string. It will be also used to get back our string. This is also a required parameter.
  • cipher_algorithm: The algorithm to encrypt our data. This is an optional parameter. By default, the value is set to ‘aes-128-cbc’.

Syntax for decryptIt():

decryptIt(encrypted_string, password, cipher_algorithm)

The parameters:

  • encrypted_string: The encrypted string of our original string. This is required.
  • password: Password that we used in the time of decryption the string. This is also required an argument.
  • cipher_algorithm: The cipher algorithm type. This is optional. But if we set it to a value in the encryptIt() function other than the default value, then we have to pass the same algorithm to get our original string successfully.

 

Before we start using the methods, first of all, we have to include the  module just like any other module as you can see below:

var edi = require('encrypt-decrypt-it');

Encrypt with the encryptIt method

Using the encryptIt() method of the encrypt-decrypt-it module, we can easily encrypt our data or string. We have to our string and a password key in this method to encrypt our data. Below is the given JavaScript code to encrypt data with this method:

encrypted_string = edi.encryptIt('Here my string', 'MY_PASSWORD');

The variable of the above code will store the encrypted data of our string “Here my string”.

Decrypt to get back our string with decryptIt() method

We can get our string or data back from the encrypted string. To do it, we have to pass the encrypted string and the same password key we used to get the encrypted string inside the decryptIt() method as the parameter.

Below is the code to decrypt and get back our original string from the encrypted string:

decrypted_string = edi.decryptIt(encrypted_string, 'MY_PASSWORD');

The above variable will return our original string “Here my string”.

Also, read:

 

Leave a Reply

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