Most of the time, a developer requires encryption and decryption for encoding a message or information in such a way that only authorized person can retrieve that information.
So, the first question that comes in our mind is "What is AES?".
- AES stands for "Advanced Encryption Standard".
- It is a tool that is used to encrypt and decrypt the simple text using AES encryption algorithm.
- This algorithm was developed by two Belgian cryptographers, Joan Daemen and Vincent Rijmen.
- AES was designed to be efficient in both, hardware and software, and supports a block length of 128 bits and key lengths of 128, 192, and 256 bits.
When to use AES Encryption
- When you want to encrypt a confidential text into a decryptable format, for example - when you need to send some sensitive data in an e-mail.
- The decryption of the encrypted text is possible only if you know the right password.
Now, try to implement the AES encryption and decryption in Angular 7. It's very easy to implement in Angular 7 with the help of crypto-js.
First, we create a new project with the following command.
- ng new EncryptionDescryptionSample
After that, install crypto.js file, by the following command.
- npm install crypto-js --save
- npm install bootstrap --save
After running the above command, check the package.json file if it is installed or not. Your file should look like this.
- <h1 class="text-center">Encrypt-Decrypt with AES</h1>
- <br>
- <div>
- <div class="row">
- <div class="col-sm-6">
- <button type="button" class="btn btn-primary btn-lg btn-block">
- Encryption
- </button>
- <br>
- <div class="form-group">
- <label for="txtTextToConvert">Plain Text</label>
- <input type="text" class="form-control" placeholder="Enter text you want to encrypt" [(ngModel)]="plainText">
- </div>
- <div class="form-group">
- <label for="txtPassword">Password</label>
- <input type="password" class="form-control" placeholder="Enter encryption password" [(ngModel)]="encPassword">
- </div>
- <textarea class="form-control" readonly rows="3">{{conversionEncryptOutput}}</textarea>
- <br>
- <button type="button" class="btn btn-success float-right" (click)="convertText('encrypt')">Encrypt</button>
- </div>
- <div class="col-sm-6">
- <button type="button" class="btn btn-dark btn-lg btn-block">
- Decryption
- </button>
- <br>
- <div class="form-group">
- <label for="txtTextToConvert">Encrypted Text</label>
- <input type="text" class="form-control" placeholder="Enter text you want to decrypt" [(ngModel)]="encryptText">
- </div>
- <div class="form-group">
- <label for="txtPassword">Password</label>
- <input type="password" class="form-control" placeholder="Enter decryption password" [(ngModel)]="decPassword">
- </div>
- <textarea class="form-control" readonly rows="3">{{conversionDecryptOutput}}</textarea>
- <br>
- <button type="button" class="btn btn-success float-right" (click)="convertText('decrypt')">Decrypt</button>
- </div>
- </div>
- </div>
Now, visit "app.component.ts" file and write the below code,
- import { Component } from '@angular/core';
- import * as CryptoJS from 'crypto-js';
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
- })
- export class AppComponent {
- title = 'EncryptionDecryptionSample';
- plainText:string;
- encryptText: string;
- encPassword: string;
- decPassword:string;
- conversionEncryptOutput: string;
- conversionDecryptOutput:string;
- constructor() {
- }
- //method is used to encrypt and decrypt the text
- convertText(conversion:string) {
- if (conversion=="encrypt") {
- this.conversionEncryptOutput = CryptoJS.AES.encrypt(this.plainText.trim(), this.encPassword.trim()).toString();
- }
- else {
- this.conversionDecryptOutput = CryptoJS.AES.decrypt(this.encryptText.trim(), this.decPassword.trim()).toString(CryptoJS.enc.Utf8);
- }
- }
- }
- import * as CryptoJS from 'crypto-js';
- ng serve --o

Tech DhanhindPosted Apr 2, 2021, 2:20 PM
How to keep secure Secretkey,because if know the secret key can able to decrypt
Apanoub AmeenPosted Feb 25, 2020, 5:43 AM
You are the best i love you man thanks for sharing this
SREERAM MANEPALLIPosted Dec 17, 2019, 2:02 AM
How to include RNCryptor in angular 6 project (plz helpme)
Andriy ZapisotskyiPosted Nov 4, 2019, 11:46 AM
Great read, Mahesh Verma! Thanks for sharing
Reena LakraPosted Jul 3, 2019, 9:48 AM
Nice article. Thanks for sharing.
Alex Jose Baltodano PaniaguaPosted Jun 26, 2019, 5:07 PM
How can I decrypt an encrypted text using crypto js in .net core 2.2?
Mohd KashifPosted May 7, 2019, 12:34 AM
Very informative. thanks for sharing.
Ajay GuptaPosted Jan 22, 2019, 3:24 AM
But is it secure?
Muthu KumarPosted Jan 18, 2019, 7:51 AM
Nice Article. Thanks for sharing.
Bhairab DuttPosted Jan 17, 2019, 12:22 AM
Nice Article......
Sharad GuptaPosted Jan 17, 2019, 12:14 AM
Nice one Mahesh Verma, I think we may pass encrypted data from APIs, the decrypted at typescript side and vice versa.
Hiten PandyaPosted Jan 16, 2019, 10:49 PM
Good article. Thanks for sharing.