Step 1:
First, let's install crypto-js in our Angular project using npm:
npm install crypto-js
npm i --save-dev @types/crypto-js
Step 2:
Once crypto-js is installed, we can import it into our Local Service (EncrDecrService):
import * as CryptoJS from 'crypto-js';
Step 3:
Now we will add two encrypt and decrypt methods to handle encryption and decryption in our service class:
Step 4:
//Private Key
key = "encrypt!135790";
//To encrypt input data
public encrypt(password: string): string {
return CryptoJS.AES.encrypt(password, this.key).toString();
}
//To decrypt input data
public decrypt(passwordToDecrypt: string) {
return CryptoJS.AES.decrypt(passwordToDecrypt, this.key).toString(CryptoJS.enc.Utf8);
}
Angular Encrypting Decrypting Data with CryptoJs | Angular 17 Tutorial | npm install crypto-js
Download source code from GitHub: github.com/anilsingh581/CryptoJS
Top comments (5)
Thanks for sharing !
Thank you for your appreciation.
Wow, such an inspirational article. Could you please write the next one about how to install npm packages?
Sure, I will do that and share it with you a link.. Thank you
Download source code from GitHub: github.com/anilsingh581/CryptoJS