Secure Your Full-Stack Applications with Encryption Best Practices
Handling sensitive data is a critical requirement in modern applications. Whether it is user passwords, payment information, personal identification data, or health records, proper encryption ensures data confidentiality and integrity. Combining Angular frontend with ASP.NET Core backend and SQL Server requires careful handling of data both in transit and at rest.
1. Understanding Data Security Layers
Encryption can be implemented at multiple layers:
| Layer | Purpose | Example Techniques |
|---|---|---|
| In Transit | Protect data while moving between frontend/backend | HTTPS (TLS), Secure WebSocket (WSS) |
| At Rest | Protect stored data in a database or storage | TDE, Column-level encryption, AES encryption |
| Application Layer | Protect sensitive fields before database insertion | AES, RSA encryption in .NET |
Best practice: Combine in-transit encryption (HTTPS) with application-layer encryption for highly sensitive fields.
2. Encrypting Data in Angular (Frontend)
Angular typically handles data before sending it to the backend. While HTTPS encrypts data in transit, some regulations require end-to-end encryption, where sensitive fields are encrypted in the browser.
Using AES Encryption in Angular
We can use the CryptoJS library for AES encryption.
npm install crypto-js
// encryption.service.tsimport { Injectable } from '@angular/core';
import * as CryptoJS from 'crypto-js';
@Injectable({ providedIn: 'root' })
export class EncryptionService {
private key = 'YOUR_SECURE_KEY'; // Use environment variable in production
encrypt(text: string): string {
return CryptoJS.AES.encrypt(text, this.key).toString();
}
decrypt(cipherText: string): string {
const bytes = CryptoJS.AES.decrypt(cipherText, this.key);
return bytes.toString(CryptoJS.enc.Utf8);
}
}
Example Usage in Component
import { Component } from '@angular/core';
import { EncryptionService } from '../services/encryption.service';
@Component({
selector: 'app-secure-form',
templateUrl: './secure-form.component.html'
})
export class SecureFormComponent {
password: string = '';
constructor(private encryptionService: EncryptionService) {}
submitForm() {
const encryptedPassword = this.encryptionService.encrypt(this.password);
// send encryptedPassword to backend via API
}
}
Frontend Best Practices:
Never store encryption keys in code, use environment variables or a secure key exchange mechanism.
Use AES-256 or higher.
Combine with HTTPS to prevent MITM attacks.
3. ASP.NET Core Backend Encryption
Backend encryption is critical for storing sensitive fields in the database.
Encrypting Fields Using AES
using System.Security.Cryptography;
using System.Text;
public class CryptoService
{
private readonly byte[] _key;
private readonly byte[] _iv;
public CryptoService(IConfiguration config)
{
_key = Encoding.UTF8.GetBytes(config["Encryption:Key"]);
_iv = Encoding.UTF8.GetBytes(config["Encryption:IV"]);
}
public string Encrypt(string plainText)
{
using var aes = Aes.Create();
aes.Key = _key;
aes.IV = _iv;
var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
var plainBytes = Encoding.UTF8.GetBytes(plainText);
var encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
return Convert.ToBase64String(encryptedBytes);
}
public string Decrypt(string cipherText)
{
using var aes = Aes.Create();
aes.Key = _key;
aes.IV = _iv;
var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
var cipherBytes = Convert.FromBase64String(cipherText);
var decryptedBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
return Encoding.UTF8.GetString(decryptedBytes);
}
}

Join the conversation! Your thoughts help the community grow.