Introduction
RSA algorithm is an asymmetric cryptography algorithm. Asymmetric actually means that it works on two different keys, i.e., public key and private key. As the name describes, the public key is given to everyone, and the private key is kept private.
We are going to encrypt and decrypt data as "The encryptRSA method encrypts large data by breaking it into smaller chunks (86 bytes), encrypting each chunk individually with RSA, and then concatenating the Base64-encoded encrypted chunks into a final string. This approach is necessary because RSA can only encrypt a limited amount of data at a time due to its key size limitations. The final result is a single string that represents the encrypted data."
Step 1. Install the jsencrypt Library.
npm install jsencrypt
Step 2. Import Forge in the component or service that you would like to use.
import * as Forge from 'node-forge';
Step 3. Complete Encryption Method.
// RSA Encryption
encryptRSA(data: any): string {
const publickey: string = ``; // Replace your own public key
const rsa = Forge.pki.publicKeyFromPem(publickey);
const jsonData = data;
const chunkSize = 86;
let finalEncrypted = '';
for (let i = 0; i < jsonData.length; i += chunkSize) {
const chunk = jsonData.substring(i, i + chunkSize);
const encryptedChunk = rsa.encrypt(chunk);
finalEncrypted += Forge.util.encode64(encryptedChunk);
}
return finalEncrypted;
}
Breakdown of the encryption method
Step 1. Forge.pki.publicKeyFromPem: Converts the PEM-encoded public key into a format that the node-forge library can use for encryption.
const rsa = Forge.pki.publicKeyFromPem(this.publickey);
Step 2
- for (let i = 0; i < jsonData.length; i += chunkSize): Iterates through the data in chunks of chunkSize (86 bytes in this case).
- jsonData.substring(i, i + chunkSize): Extracts a chunk of the data starting from index i with a length of chunkSize.
- rsa.encrypt(chunk): Encrypts the extracted chunk using the RSA public key.
- Forge.util.encode64(encryptedChunk): Encodes the encrypted chunk into Base64 format. This is necessary because RSA encryption produces binary data, which might not be suitable for text-based transmission or storage.
- finalEncrypted = finalEncrypted + Forge.util.encode64(encryptedChunk): Appends the Base64-encoded encrypted chunk to the final result.
for (let i = 0; i < jsonData.length; i += chunkSize) {
const chunk = jsonData.substring(i, i + chunkSize);
const encryptedChunk = rsa.encrypt(chunk);
finalEncrypted += Forge.util.encode64(encryptedChunk);
}
Output Sample for Encryption

Decryption Complete Method
// RSA Decryption
decryptionRSA(value: any): any {
var privatekey: string = ``; // replace your private key
const rsa = Forge.pki.privateKeyFromPem(this.privatekey);
var ctBytes = Forge.util.decode64(value);
var plaintextBytes = rsa.decrypt(ctBytes);
return plaintextBytes.toString();
}
Output Sample for Decryption

Complete encryption and decryption Service
import { Injectable } from '@angular/core';
import * as CryptoJS from 'crypto-js';
import * as Forge from 'node-forge';
@Injectable({
providedIn: 'root'
})
export class EncryptionService {
private secretKey: string = ''; // Replace with a secure key
private Vector: string = '';
private publickey: string = ``;
private privatekey: string = ``;
constructor() {}
// AES encryption
encrypt(value: string): string {
return CryptoJS.AES.encrypt(value, this.secretKey, {
iv: CryptoJS.enc.Utf8.parse(this.Vector),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
}).toString();
}
encryptObject(value: any): string {
return CryptoJS.AES.encrypt(JSON.stringify(value), this.secretKey, {
iv: CryptoJS.enc.Utf8.parse(this.Vector),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
}).toString();
}
// AES Decryption
decrypt(encryptedText: string): string {
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);
}
// Encryption using CBC Triple DES
encryptUsingTripleDES(res: any, typeObj: boolean): string {
const data = typeObj ? JSON.stringify(res) : res;
const keyHex = CryptoJS.enc.Utf8.parse(this.secretKey);
const iv = CryptoJS.enc.Utf8.parse(this.Vector);
const mode = CryptoJS.mode.CBC;
const encrypted = CryptoJS.TripleDES.encrypt(data, keyHex, { iv, mode });
return encrypted.toString();
}
// Decryption using CBC Triple DES
decryptUsingTripleDES(encrypted: string): string {
const keyHex = CryptoJS.enc.Utf8.parse(this.secretKey);
const iv = CryptoJS.enc.Utf8.parse(this.Vector);
const mode = CryptoJS.mode.CBC;
const decrypted = CryptoJS.TripleDES.decrypt(encrypted, keyHex, { iv, mode });
return decrypted.toString(CryptoJS.enc.Utf8);
}
// RSA Encryption
encryptRSA(data: any): string {
const rsa = Forge.pki.publicKeyFromPem(this.publickey);
const jsonData = data;
const chunkSize = 86;
let finalEncrypted = '';
for (let i = 0; i < jsonData.length; i += chunkSize) {
const chunk = jsonData.substring(i, i + chunkSize);
const encryptedChunk = rsa.encrypt(chunk);
finalEncrypted += Forge.util.encode64(encryptedChunk);
}
return finalEncrypted;
}
encryptObjectRSA(data: any): string {
const rsa = Forge.pki.publicKeyFromPem(this.publickey);
const jsonData = JSON.stringify(data);
const chunkSize = 86;
let finalEncrypted = '';
for (let i = 0; i < jsonData.length; i += chunkSize) {
const chunk = jsonData.substring(i, i + chunkSize);
const encryptedChunk = rsa.encrypt(chunk);
finalEncrypted += Forge.util.encode64(encryptedChunk);
}
return finalEncrypted;
}
// RSA Decryption
decryptionRSA(value: any): any {
const rsa = Forge.pki.privateKeyFromPem(this.privatekey);
const ctBytes = Forge.util.decode64(value);
const plaintextBytes = rsa.decrypt(ctBytes);
return plaintextBytes.toString();
}
}
Join the conversation! Your thoughts help the community grow.