Introduction
Nowadays, clients want to secure their websites. They want no one to be able to see the whole payload or id parameter value that we pass during an API call. Using the methods discussed here, you can get the exact solution.
In this article, I have explained everything that is more useful for security.
First, I am going to explain Encrypt and Decrypt whole payload or any id using Angular 11(front end) and ASP.NET CORE (API).
I have used AES256 algorithm in both Angular and ASP.NET CORE. in angular side using interceptor, passing the payload or id with encryption and using same algorithm, getting payload or ids in ASP.NET CORE using middleware that I will explain step by step.
Make sure that if we use any algorithm for encryption from angular or any other front-end framework, we must use the same algorithm for decryption.
Let me explain step by step, starting with Angular code.
First, you have to create a project. If not, create and do all processes like CRUD or any functionality that use GET and POST, else you can add to your current project also.
Angular Code
Step 1
Install the crypto js using the npm terminal. Use the below command to install the crypto js:
npm i crypto-js
I have used 4.1.1 version. You can use according to your angular version.
Once we've installed Crypto Js, we have to create one service to encrypt the payload or parameter.
Step 2
Create the service for Encrypt-Decrypt payload with the AES256 algorithm.
For Encrypt KEY and Encrypt IV, we have to obtain a unique 16-number code that we will use in encrypt, and also use same key and IV in asp.net code side for decrypt.
Here I am using same id for both. You can set them differently, but make sure to use the same key and IV to decrypt the code.
environment.EncryptKey = 1203199320052021
environment.EncryptIV = 1203199320052021
Here I have created a service and given it the name encrypt-decrypt.service.ts.
import { Injectable } from '@angular/core';
import * as CryptoJS from 'crypto-js';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
})
export class EncryptDecryptService {
private key = CryptoJS.enc.Utf8.parse(environment.EncryptKey);
private iv = CryptoJS.enc.Utf8.parse(environment.EncryptIV);
constructor() {}
// Methods for the encrypt and decrypt Using AES
encryptUsingAES256(text): any {
var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(text), this.key, {
keySize: 128 / 8,
iv: this.iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
}
decryptUsingAES256(decString) {
var decrypted = CryptoJS.AES.decrypt(decString, this.key, {
keySize: 128 / 8,
iv: this.iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
}
So, the above code is for encrypting and decrypting the payload and parameter. For these functions we will use the interceptor method.
HTTP Interceptors is a special type of angular service that we can implement. It's used to apply custom logic to the central point between the client-side and server-side outgoing/incoming HTTP request and response. Keep in mind that the interceptor wants only HTTP requests.
Let’s create an interceptor.
Step 3
Create the interceptor file to generate encrypted payload before passing it into API service call.
I created interceptor with encrypt-decrypt-interceptor.ts name.
See the below code of interceptor method:
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { EncryptDecryptService } from './services/encrypt-decrypt.service';
import { Observable } from 'rxjs/Observable';
import { environment } from 'src/environments/environment';
@Injectable()
export class EncryptDecryptAuthInterceptor implements HttpInterceptor {
constructor(private encryptDecryptService: EncryptDecryptService, ) {}
// If you want to some exclude api call from Encryption then add here like that.
// environment.basUrl is your API URL
ExcludeURLList = [
environment.baseUrl + "/api/Common/commonFileuploaddata",
environment.baseUrl + "/api/Users/UploadProfilePicture",
environment.baseUrl + "/api/Common/downloadattachedfile"
];
intercept(req: HttpRequest < any > , next: HttpHandler): Observable < HttpEvent < any >> {
let exludeFound = this.ExcludeURLList.filter(element => {
return req.url.includes(element)
});
// We have Encrypt the GET and POST call before pass payload to API
if (!(exludeFound && exludeFound.length > 0)) {
if (req.method == "GET") {
if (req.url.indexOf("?") > 0) {
let encriptURL = req.url.substr(0, req.url.indexOf("?") + 1) + this.encryptDecryptService.encryptUsingAES256(req.url.substr(req.url.indexOf("?") + 1, req.url.length));
const cloneReq = req.clone({
url: encriptURL
});
return next.handle(cloneReq);
}
return next.handle(req);
} else if (req.method == "POST") {
if (req.body || req.body.length > 0) {
const cloneReq = req.clone({
body: this.encryptDecryptService.encryptUsingAES256(req.body)
});
return next.handle(cloneReq);
}
let data = req.body as FormData;
return next.handle(req);
}
}
return next.handle(req);
}
}
I want to mention specifically that if you want to exclude API calls from encryption, you have to just add an API URL into ExcludeURLList as shown in Interceptor. Our project has many methods, or API calls, that are unnecessary to encrypt. Then, we can manage in our way. Suppose here I mention that some API calls exclude lists that upload images that are related. So for uploading images, encryption not needed. So, I added it under the exclude list.
Now need to configure it into a module file so that whenever we call the API it will call the interceptor before API and encrypt the payload. Then it will pass into API (GET or POST).



Aravind RaviPosted Sep 4, 2024, 5:57 AM
If we kept the Encryption Keys in Angular Typescript it will be exposed in browser developer tools?
abu arqamPosted Aug 27, 2024, 10:12 AM
I am unable to download sample code, please update again.
Mayuri UttekarPosted Jul 9, 2024, 4:38 AM
How to encrypt web api response?
Ganesh BabuPosted Jan 10, 2024, 6:10 PM
Hi , Thanks for the article, i am getting null object in post API after decrypting the input. can you please assist
Deepak ThukralPosted Nov 27, 2023, 9:20 AM
Encryption is working but not able to hit api after decryption
alfonsoPosted Nov 23, 2023, 12:20 AM
Can't decrypt JSON body in .NET Web API .
abc mvcPosted May 10, 2023, 12:17 PM
Unable to decrypt json body from .net core webapi
Santiago CasallasPosted Jan 20, 2023, 1:58 PM
When you say "no one can see the original payload/parameter value" , it also applies to network sniffers or HTTP protocol monitors ??
sandeep singh solankiPosted Dec 14, 2022, 12:56 PM
Unable to download