AES Encryption/Decryption With Angular 7

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. 

In this article, I will discuss the implementation of AES encryption and decryption with Angular 7 while developing an application.

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.

  1. ng new EncryptionDescryptionSample  

After that, install crypto.js file, by the following command.

  1. npm install crypto-js --save  
For better UI, we also install bootstrap by the following command.
  1. 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.

AES Encryption/Decryption with Angular 7 
Now, go to "app.component.html" file and replace the existing code with the below code.
  1. <h1 class="text-center">Encrypt-Decrypt with AES</h1>  
  3. <br>  
  4. <div>  
  5.   <div class="row">  
  6.     <div class="col-sm-6">  
  7.       <button type="button" class="btn btn-primary btn-lg btn-block">  
  8.         Encryption  
  9.       </button>  
  10.       <br>  
  11.       <div class="form-group">  
  12.         <label for="txtTextToConvert">Plain Text</label>  
  13.         <input type="text" class="form-control" placeholder="Enter text you want to encrypt" [(ngModel)]="plainText">  
  14.       </div>  
  16.       <div class="form-group">  
  17.         <label for="txtPassword">Password</label>  
  18.         <input type="password" class="form-control" placeholder="Enter encryption password" [(ngModel)]="encPassword">  
  19.       </div>  
  20.       <textarea class="form-control" readonly rows="3">{{conversionEncryptOutput}}</textarea>  
  21.       <br>  
  22.       <button type="button" class="btn btn-success float-right" (click)="convertText('encrypt')">Encrypt</button>  
  23.     </div>  
  24.     <div class="col-sm-6">  
  25.       <button type="button" class="btn btn-dark btn-lg btn-block">  
  26.         Decryption  
  27.       </button>  
  28.       <br>  
  29.       <div class="form-group">  
  30.         <label for="txtTextToConvert">Encrypted Text</label>  
  31.         <input type="text" class="form-control" placeholder="Enter text you want to decrypt" [(ngModel)]="encryptText">  
  32.       </div>  
  34.       <div class="form-group">  
  35.         <label for="txtPassword">Password</label>  
  36.         <input type="password" class="form-control" placeholder="Enter decryption password" [(ngModel)]="decPassword">  
  37.       </div>  
  38.       <textarea class="form-control" readonly rows="3">{{conversionDecryptOutput}}</textarea>  
  39.       <br>  
  40.       <button type="button" class="btn btn-success float-right" (click)="convertText('decrypt')">Decrypt</button>  
  41.     </div>  
  42.   </div>  
  43. </div>  

Now, visit "app.component.ts" file and write the below code,

  1. import { Component } from '@angular/core';  
  2. import * as CryptoJS from 'crypto-js';  
  4. @Component({  
  5.   selector: 'app-root',  
  6.   templateUrl: './app.component.html',  
  7.   styleUrls: ['./app.component.css']  
  8. })  
  9. export class AppComponent {  
  10.   title = 'EncryptionDecryptionSample';  
  12.   plainText:string;  
  13.   encryptText: string;  
  14.   encPassword: string;  
  15.   decPassword:string;  
  16.   conversionEncryptOutput: string;  
  17.   conversionDecryptOutput:string;  
  19.   constructor() {  
  20.   }  
  21.   //method is used to encrypt and decrypt the text  
  22.   convertText(conversion:string) {  
  23.       if (conversion=="encrypt") {  
  24.         this.conversionEncryptOutput = CryptoJS.AES.encrypt(this.plainText.trim(), this.encPassword.trim()).toString();  
  25.       }  
  26.       else {  
  27.         this.conversionDecryptOutput = CryptoJS.AES.decrypt(this.encryptText.trim(), this.decPassword.trim()).toString(CryptoJS.enc.Utf8);  
  29.     }  
  30.   }  
  31. }  
In the above code, we used the Encrypt method of AES and passed our plain text with a password to encrypt the string. Similarly, we used the Decrypt method of AES and passed our encrypted text with a password to decrypt the string. Be sure that both times, your password is the same. For using both of these methods, you need to import "CryptoJS" from crypto-js.
  1. import * as CryptoJS from 'crypto-js';  
Now, let us run the project by using the following command.
  1. ng serve --o  
Here is the output.
AES Encryption/Decryption with Angular 7 


Now, we have learned what AES encryption and decryption are and how we can implement this feature with Angular. For practice purposes, the sample project called “EncryptionDecryptionSample” is attached which covers all the pieces of code mentioned in this article. We can download and modify this as per our different requirements. 
All the queries related to this article and sample project are always welcome. Thanks for reading.