Introduction

AES is a symmetric key algorithm, meaning the same key is used for both encryption and decryption. This is in contrast to asymmetric encryption, where a public key is used for encryption and a private key for decryption.

Key Sizes

AES supports three different key lengths.

Encryption and decryption in angular

Here’s a basic guide on how to implement AES encryption and decryption in Angular using the crypto-js library.

1. Install crypto-js

First, you need to install the crypto-js library.

npm install crypto-js

2. Import crypto-js

Import the necessary components from crypto-js in your Angular service or component.

import * as CryptoJS from 'crypto-js';

3. Encryption and Decryption

You can create utility methods for encryption and decryption using AES.

Encryption

  private secretKey: string = ''; // Replace with a secure key
  private Vector:string =''; // Replace with vector 
 return CryptoJS.AES.encrypt(value, this.secretKey,{
      iv: CryptoJS.enc.Utf8.parse(this.Vector),
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7,  
      
    }).toString();

Decryption

   const decrypted = CryptoJS.AES.decrypt(encryptedText, this.secretKey, {
      iv:  CryptoJS.enc.Utf8.parse(this.Vector),
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7,
  
    });
    return decrypted.toString(CryptoJS.enc.Utf8);

Different types of the mode

Cipher Block Chaining (CBC) Mode

Electronic Codebook (ECB) Mode

Cipher Feedback (CFB) Mode

Output Feedback (OFB) Mode

Counter (CTR) Mode

Different types of padding

Key Encoding

Defines the encoding format used for keys and data.

Output Format

Determines how the encrypted data is represented.

Output Sample Encrypted text.

Encrypted text