Skip to main content

Posts

Showing posts with the label Angular 17 Encrypting and Decrypting Data with CryptoJs

Angular 17 Encrypting and Decrypting Data with Crypto-js

First, let's install crypto-js in our Angular project using NPM: npm install crypto-js npm i --save-dev @types/crypto-js Once CryptoJs is installed, we can import it into our Local Service (EncrDecrService): import * as CryptoJS from 'crypto-js'; Now we will add two encrypt and decrypt methods to handle encryption and decryption in our service class: import { Injectable } from '@angular/core'; import * as CryptoJS from 'crypto-js'; @Injectable({   providedIn: 'root' }) export class EncrDecrService {   //Private Key   key = "encrypt!135790";   //constructor   constructor() { }     //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(Crypto...