How to Encrypt and Decrypt in Node JS using Crypto

In this article, you’ll learn how to use the Node.js crypto module to perform cryptographic operations on data. I'll show you how to encrypt data with a secret key and then decrypt it using the same secret key when required.

Create a new project

mkdir encr_decr && cd encr_decr
npm init -y

Now install the crypto module

npm install crypto --save

Now make app.js.

Add following code to app.js

var Crypto = require('crypto');
var secret_key = 'fd85b494-aaaa';
var secret_iv = 'smslt';
var encryptionMethod = 'AES-256-CBC';
var key = Crypto.createHash('sha512').update(secret_key, 'utf-8').digest('hex').substr(0, 32);var iv = Crypto.createHash('sha512').update(secret_iv, 'utf-8').digest('hex').substr(0, 16);var encryptedMessage = encrypt_string("hello", encryptionMethod, key, iv);console.log(encryptedMessage); // output : L2dOZjlDVmxoSDNWdmpVMkNGd0JEdz09var decrptMessage = decrypt_string(encryptedMessage, encryptionMethod, key , iv);console.log(decrptMessage); //output : hellofunction encrypt_string(plain_text, encryptionMethod, secret, iv) {
var encryptor = Crypto.createCipheriv(encryptionMethod, secret, iv);
var aes_encrypted = encryptor.update(plain_text, 'utf8', 'base64') + encryptor.final('base64');
return Buffer.from(aes_encrypted).toString('base64');
};
function decrypt_string(encryptedMessage, encryptionMethod, secret, iv) {
const buff = Buffer.from(encryptedMessage, 'base64');
encryptedMessage = buff.toString('utf-8');
var decryptor = Crypto.createDecipheriv(encryptionMethod, secret, iv);
return decryptor.update(encryptedMessage, 'base64', 'utf8') + decryptor.final('utf8');
};

Conclusion

Developer by profession . Youtube channel : https://www.youtube.com/channel/UCOWT2JvRnSMVSboxvccZBFQ