Crypto is a module in Node.js which deals with an algorithm that performs data encryption and decryption. This is used for security purpose like user authentication where storing the password in Database in the encrypted form.

Crypto module provides set of classes like hash, HMAC, cipher, decipher, sign, and verify. The instance of that class is used to create Encryption and Decryption. Node.js cannot create a class object using the new keyword.

Topics Covered

  • Installing  crypto module
  • Requiring crypto module
  • Cipher algorithm – getCiphers()
  • Encryption – crypto.createCipher()
  • Decryption – crypto.createDecipher()

To install Crypto module in Node.js in following way.

c:\Users\Magnet Brains\Desktop\project>npm install crypto --save

To use the crypto module in Node.js then first require crypto.

var crypto = require('crypto');

To get all Cipher algorithms that support crypto in a Node.js run following command.

Run Command

c:\Users\Magnet Brains\Desktop\project>node
> require("crypto").getCiphers()

Result

c:\Users\Magnet Brains\Desktop\project>node
> require("crypto").getCiphers()
[ 'aes-128-cbc',
'aes-128-ccm',
'aes-128-cfb',
'aes-128-cfb1',
'aes-128-cfb8',
'aes-128-ctr',
'aes-128-ecb',
'aes-128-gcm',
'aes-128-ofb',
'aes-128-xts',
'aes-192-cbc',
'aes-192-ccm',
'aes-192-cfb',
'aes-192-cfb1',
...  more items ]

Encryption

The process of transforming readable text into the unreadable format called Encryption.
In Node.js to create encryption then following method is used.

crypto.js

const crypto = require('crypto');
const password ='crypto@123';
const cipher = crypto.createCipher('aes128', 'a password');
var encrypted = cipher.update(password, 'utf8', 'hex');
encrypted += cipher.final('hex');
console.log(encrypted);

Run Command

c:\Users\Magnet Brains\Desktop\project>node crypto.js

Result

c:\Users\Magnet Brains\Desktop\project>node crypto.js
6ac2b3b08ce481c8016ee2067ba44081

Cipher is a class in which crypto.createCipher() is a method which creates an instance of that class. The instance of that class is used to transform plain readable text into the unreadable format.

In Example string ‘crypto@123’ is a plain password and using .update() and .final() method with ‘aes128’ algorithm to encrypt that plain password in coded format. i.e ‘6ac2b3b08ce481c8016ee2067ba44081’.

Decryption

The process of converting a readable text into the unreadable format is called Decryption.
In Node.js to create Decryption then following method is used.

crypto.js

const crypto = require('crypto');
const encrypt_password = '6ac2b3b08ce481c8016ee2067ba44081';
const decipher = crypto.createDecipher('aes128','a password');
var decrypted = decipher.update(encrypt_password,'hex', 'utf8');
decrypted += decipher.final('utf8');
console.log(decrypted);

Run Command

c:\Users\Magnet Brains\Desktop\project>node crypto.js

Result

c:\Users\Magnet Brains\Desktop\project>node crypto.js
crypto@123

Decipher is a class in which crypto.createDecipher() is a method which creates an instance of that class. The instance of that class is used to transform encrypted data into the human-readable format.

In Example string ‘6ac2b3b08ce481c8016ee2067ba44081’ is a encrypted password and using .update() and .final() method with ‘aes128’ algorithm to decrypt that encrypted password in readable format i.e ‘crypto@123’.

Learn More: