In this part, we will learn about doing encryption and decryption using cryto module of node.js symmetrically as well as asymmetrically.
Data that can be read and understood easily is known as Plain text. The problem with plain text is that everyone can read it.
But sometimes we don't want everyone to have the access to read the data i.e. the confidentiality of the data needs to be maintained.
Encryption :
Encryption deals with providing confidentiality to the data so that only authorize personnel have access to the data. In encryption , Plain text is
transfromed to an unintelligible text with the help of key
and algorithm
which we
can read but can not understand due to which the confidentiality of the data is protected. This unintelligible text is known as Cipher text
.
Decryption :
Decryption is the process of rendering the data , so that it can be changed into a human or machine readable and understandable form.
It takes the cipher text as input and converts it into the plain text with the help of key
and algorithm
.
There are 2 ways to encrypt and decrypt the content which are explained in detail below :
createCipher
and decryption using createDecipher
inbuilt methods : Same key is used
for both encryption and decryption.
var crypto = require('crypto'),algorithm = 'aes-256-ctr',password = 'RJ23edrf';
//Here "aes-256-cbc" is the advance encryption standard we are using for encryption.
//Text is the Confidential data which we need to encrypt using 'password'(Key).
function encrypt(text){
var cipher = crypto.createCipher(algorithm,password)
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');
return crypted;
}
//Here "aes-256-cbc" is the advance encyption standard we used for encrytion.
//Text is the Cipher which we need to decrypt using 'password'(Key).
function decrypt(text){
var decipher = crypto.createDecipher(algorithm,password)
var dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8');
return dec;
}
//Actual content
var text = "Nodejsera for all web development languages";
//Calling the encrypt function and printing the encrypted content
var e = encrypt(text);
console.log(e);
//calling the decrypt function and printing the decrypted content
var d = decrypt(e);
console.log(d);
>node enc-pub-dec-priv.js
Also known as public key encryption, we use differnt key for decryption from the one we used for encryption. There are 2 possibilities which are explained with code snippets below :
/**
Example of Asymmetric encryption
Encrypting using public key and decrypting using private key
File Name : enc-pub-dec-priv.js
Author : @nodejsera
**/
//Including the required modules
var crypto = require('crypto');
var fs = require('fs');
//Reading the Public Key
pubK = privK = fs.readFileSync('pub.key').toString();
//Passing the text to be encrypted using private key
var buf = Buffer.from('This is secret code', 'utf8');
//Encrypting the text
secretData = crypto.publicEncrypt(pubK, buf);
//printing the encrypted text
console.log(secretData.toString('utf8'));
//reading the Private key
privK = {
key: fs.readFileSync('priv.key').toString(),
passphrase: 'nodejsera'
}
//decrypting the text using public key
origData = crypto.privateDecrypt(privK, secretData)
//Printing the original content
console.log(origData.toString());
>node enc-pub-dec-priv.js
/**
Example of Asymmetric encryption
Encrypting using private key and decrypting using public key
File Name : enc-priv-dec-pub.js
Author : @nodejsera
**/
//Including the required modules
var crypto = require('crypto');
var fs = require('fs');
//Reading the Private Key
privK = {
key: fs.readFileSync('priv.key').toString(),
passphrase: 'nodejsera'
}
//Passing the text to be encrypted using private key
var buf = Buffer.from('rishabh', 'utf8');
//Encrypting the text
secretData = crypto.privateEncrypt(privK, buf);
//printing the encrypted text
console.log(secretData.toString('utf8'));
//reading the Public key
pubK = fs.readFileSync('pub.key').toString();
//decrypting the text using public key
origData = crypto.publicDecrypt(pubK, secretData)
//Printing the original content
console.log(origData.toString());
>node enc-priv-dec-pub.js
In this chapter of 30 days of node tutorial series, we learned about how we can use
crypto
module in node.js for encryption and decryption. We learned about what is encryption , what is decryption , why encryption is necessary ,
how we can perform symmetric or private key encryption and decryption.
Also we learned about public key or asymmetric encryption and decryption.