Here is an example of the decryption function which can be used for the decryption of the encrypted data
const encryptedKey1 = fs.readFileSync( path.resolve(__dirname, “../../../../keys/encryptkey.txt”), “utf8”);
const EN_PATH = path.resolve(__dirname, “../../../../keys/encrypt.txt”);
const EN_KEY = fs.readFileSync(EN_PATH, “utf8”);
var key = await mycrypto.decryptViaPrivateKey(encryptedKey1, GTEN_PRIKEY_PATH, PASS);
const aescbc = new aesjs.ModeOfOperation.cbc(aesjs.utils.utf8.toBytes(key), “”);
let _decryptedText = aescbc.decrypt(Buffer.from(EN_KEY, “base64”));
_decryptedText = aesjs.utils.utf8.fromBytes(_decryptedText);
//console.log(_decryptedText);
let _validCharacters = parseInt((_decryptedText.length / 16).toString());
_decryptedText = _decryptedText.substr(_decryptedText.length + 1 – _validCharacters * 16, _decryptedText.length);
_decryptedText = ‘{‘ + _decryptedText;
//console.log(_decryptedText);
let _decryptedJson = JSON.stringify(_decryptedText.split(/r/).join(“”).split(/n/).join(“”).split(/t/).join(“”).split(“u0006“).join(“”).split(” “).join(“”));
_decryptedJson = JSON.parse(_decryptedJson);
console.log(“_decryptedJson”, _decryptedJson);
response.send({“data”:_decryptedJson});
The encryption uses an key from the env and some data encryption
Decryption logic and code
const crypto = require(‘crypto’)
const path = require(‘path’)
const fs = require(‘fs’)
module.exports = {
encryptViaPrivateKey: async function (input, pathToKey, passphrase) {
console.log(“==> encryptViaPrivateKey”);
const absolutePath = path.resolve(pathToKey);
const key = fs.readFileSync(absolutePath, ‘utf8’);
const buffer = Buffer.from(input, ‘utf8’);
const output = crypto.privateEncrypt(
{
key: key.toString(),
passphrase: passphrase,
padding: crypto.constants.RSA_PKCS1_PADDING
},
buffer,
)
return output.toString(‘base64’);
},
decryptViaPrivateKey: async function (input, pathToKey, passphrase) {
console.log(“==> decryptViaPrivateKey”);
const absolutePath = path.resolve(pathToKey);
const key = fs.readFileSync(absolutePath, ‘utf8’);
const buffer = Buffer.from(input, ‘base64’);
const output = crypto.privateDecrypt(
{
key: key.toString(),
passphrase: passphrase,
padding: crypto.constants.RSA_PKCS1_PADDING
},
buffer,
)
return output.toString(‘utf8’);
},
encryptViaPublicKey: async function (input, pathToKey, passphrase) {
console.log(“==> encryptViaPublicKey”);
const absolutePath = path.resolve(pathToKey);
const key = fs.readFileSync(absolutePath, ‘utf8’);
const buffer = Buffer.from(input, ‘utf8’);
const output = crypto.publicEncrypt(
{
key: key.toString(),
passphrase: passphrase,
padding: crypto.constants.RSA_PKCS1_PADDING
},
buffer,
)
return output.toString(‘base64’);
},
decryptViaPublicKey: async function (input, pathToKey, passphrase) {
console.log(“==> decryptViaPublicKey”);
const absolutePath = path.resolve(pathToKey);
const key = fs.readFileSync(absolutePath, ‘utf8’);
const buffer = Buffer.from(input, ‘base64’);
const output = crypto.publicDecrypt(
{
key: key.toString(),
passphrase: passphrase,
padding: crypto.constants.RSA_PKCS1_PADDING
},
buffer,
)
return output.toString(‘utf8’);
},
}
This crypto module can be used for the encryption and decryption of the DAta based on the RES- AES Keys