You need to generate the pair of public and private key. Provide public key to Altery. Use private key to decrypt the content of encrypted messages
const crypto = require('crypto');
const message = '... content of message ...';
const sign = '... value of x-sign header ...';
const privateKey = '... private key ...';
const decryptDataWithPrivateKey = (encryptedData, privateKey) => {
const ciphertext = Buffer.from(encryptedData, 'base64');
const decryptedData = crypto.privateDecrypt(
{
key: privateKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
},
ciphertext
);
return decryptedData.toString();
}
const decryptContent = (messageBase64, keyBase64) => {
const ivSize = 16;
const aesKey = Buffer.from(keyBase64, 'utf8');
const iv = Buffer.from(messageBase64.slice(0, ivSize), 'utf8')
const encryptedTextBase64 = messageBase64.slice(ivSize, messageBase64.length);
const decipher = crypto.createDecipheriv('aes-256-cbc', aesKey, iv);
const decrypted = decipher.update(encryptedTextBase64, 'base64', 'utf8');
return decrypted + decipher.final('utf8');
}
const decryptedSecret = decryptDataWithPrivateKey(sign, privateKey);
const content = decryptContent(message, decryptedSecret);
console.log('Decrypted content:');
console.log(content);