Data Encryption And Decryption Using Cryptojs In Clientside

Posted By : Shivam Gupta | 28-Feb-2018

When any user login with login credentials to their web application. Then some credential are stored in local storage. For user data security purpose encryption of such credentials are very important.
 
crypto js: javascript provide such library for encrypting and decrypting of data.
 
Installation of the crypto library:
npm install crypto-js --save
this will install and save crypto js in node modules and save crypto js in package.json for further use.
 
Use of crypto js library: - 
There is much technique are available for encryption or decryption of any data.
MD5: - MD5 is a very useful hash function. It's been used in a variety of security applications. The main functionality of MD5 is to check the integrity of files.
example: -
var temp = CryptoJS.MD5("data for encryption");
 
SHA-1: -
she is also very important encryption technique and it is also used in very different security applications. the implementation of sha for any application is given below-
ex: -var hash = CryptoJS.SHA1("Message");
 
SHA-2: - it provide more security than sha1, because sha 256 is one of the four variants in sha-2  set.
ex. - var hash = CryptoJS.SHA("Message");
 
SHA-3: - sha3 is efficient security method, it is a winner of a five-year competition to select a new cryptographic hash algorithm.
ex. - var hash = CryptoJS.SHA("Message");
 
RABBIT: - it is cipher method which is finalist in stream cipher, and this method is evaluted after 3.5 year process in all 22 mehods.
ex: - var encrypted = CryptoJS.Rabbit.encrypt("Message", "Secret Passphrase");
          var decrypted = CryptoJS.Rabbit.decrypt(encrypted, "Secret Passphrase");
 
DES: - it is also very important method for encryption purpose, but this method is not so much secure due to small key size. this method published by Federal Information Processing
Standard. example are given below -
var encrypted = CryptoJS.DES.encrypt("Message", "Secret Passphrase");
var decrypted = CryptoJS.DES.decrypt(encrypted, "Secret Passphrase");
 
AES: - AES(Advanced Encryption Standard) technology use 'pass phrase' for encryption and decryption any content data.
ex: - for encryption of any data we encrypt function, which is given below.

               temp = localStorage.getItem(key);
        if (temp) {
            let bytes = CryptoJS.AES.decrypt(temp, 'secret key 123');
            let decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
            return decryptedData;
        } else {
            return null
        }
 
       

About Author

Author Image
Shivam Gupta

Shivam is a versatile UI developer with html, css, scss, js, angularjs technologies. He likes chess, cricket and reading book.

Request for Proposal

Name is required

Comment is required

Sending message..