Encryption and Decryption using Crypto-js in Angularjs and Vector

This quick angular tutorial help to encrypt and decrypt variable using crypto.js. I am using Angularjs Crypto angular plugin for encryption and decryption.Crypto.js is very powerful library which is is used to encrypt and decrypt variable, forms data and any header parameters.You can create encrypted string using your salt code so that user could not decrypt your data.

Normally programmer are using BASE64 string which can decrypt easily without any effort because they are using same salt or algorithm not user defined.There are a lot of online website providing functionality to decrypt BASE64 string.

salt string is a user defined public key which is uses for encryption and decryption data.So both party have same public key(salt) to encrypt and decrypt data.


We are using below Files,The details are:

Index.html - This file will responsible to display encrypted and decrypted string.
app.js - This file responsible to encrypt and decrypt string using crypto.js and send data to html view file.
crypto lib - This will contains all crypto-js library files and use to encrypt/decrypt data.

Step 1: Include all necessary library files in header of index.html.

Step 2: Created UI for display source,encrypted and decrypted string.

Step 3: We will create angularjs application controller file and inject all dependency including crypto.js .

Here we configured crypto using .run() angular method.We have defined base64 salt and vector on-load of angular application.We have passed source string and encrypted using AES algorithm.

You can see that i am using CryptoJS.AES.encrypt() method for encrypt data and create ciphertext of encrypted data.You can send this encrypted data in request to server.

You can decrypt this data using CryptoJS.AES.decrypt() method.First of all You need to create cypher params using encrypted ciphertext and then pass this cypher params with vector string to crypto-js decrypt method.

You can download source code and Demo from below link.