Hi,
We are calling an API from OutSystems that requires a few input parameters to be sent encrypted using the AES-256 CBC technique. API is decrypting the encrypted input parameter with the help of node js code using crypto module.
Node js Code is attached in the text file for encryption and decryption that API is using also you can check from here as well.
In order to encrypt the API's input parameter and enable the API to decode the code, I want assistance obtaining the attached node JS compatible encryption code in OutSystems.
I have already attempted to use the Crypto JS JavaScript library in OutSystems and created a application (URL) and have succeeded in encrypting a parameter; however, the API that uses the node.js Crypto module does not decrypt the encrypted parameter and giving an error.
Error: Decryption error in Key with Params: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
OutSystems Encryption Code:
function encryptWithParamKey(text, secretKey) { try { let iv = CryptoJS.lib.WordArray.random(IV_LENGTH); let key = CryptoJS.enc.Hex.parse(secretKey); let encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(text), key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); return iv.toString(CryptoJS.enc.Hex) + ":" + encrypted.ciphertext.toString(CryptoJS.enc.Hex); } catch (error) { console.error(`Encryption error in Key with Params: ${error.message}`); }}
Calling above function in JS:
var encryptedRole = encryptWithParamKey("Value to encrypt","Same secret code as utilized for decryption"); $parameters.RoleEncryptedValue = encryptedRole? encryptedRole: "";
Need help in encryption code so that API which uses node js code can decrypt the passed input parameter.
If you're familiar with OutSystems Extentions/C#, you could use this code to create the encryptor on the OS side. this way, the key will be secret, too.
https://gist.github.com/mhingston/a47caa21298950abc4d8422d98b7437e#file-aes-cs
Hi Damian,
Thanks for your reply. I tried this .Net code as you suggested but still facing the same problem. Node JS code is not able to decrypt the value encrypted by this .Net code.
This issue has been resolved. the only change I need to in JavaScript function is to encode the secret key with CryptoJS.enc.Utf8.parse() method instead of CryptoJS.enc.Hex.parse() method and remove the CryptoJS.enc.Utf8.parse() encoding from the text.
function encryptWithParamKey(text, secretKey) {
try {
let iv = CryptoJS.lib.WordArray.random(IV_LENGTH);
let key = CryptoJS.enc.Hex.parse(secretKey);
let encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(text), key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return iv.toString(CryptoJS.enc.Hex) + ":" + encrypted.ciphertext.toString(CryptoJS.enc.Hex);
} catch (error) {
console.error(`Encryption error in Key with Params: ${error.message}`);
}
Thanks for support.