javascriptaesibm-datapowerapi-gatewayapiconnect

AES Encryption Decryption in API Connect Gateway Script JSON Datapower


How can I use aes encrption/decryption in api connect gateway script.. Below is the process i tried and the error I am getting help me understanding this issue

const crypto = require('crypto');
var encryptionKey = '0123456789abcd0123456789';
var iv = '12345678';
var plainText = 'Testing';
var cipher = crypto.createCipheriv('aes128-cbc',encryptionKey,Buffer.from(iv, 'utf8'));
var ciph = cipher.update(plainText,'utf8','hex');
consle.error(cipher.final('hex'));

Response ---Error "Named shared secret key '0123456789abcd0123456789' not found"

Can someone share me script for encryption and decryption for aes algorithm?


Solution

  • From the Node.Js Documentation

    The key is the raw key used by the algorithm and iv is an initialization vector. Both arguments must be 'utf8' encoded strings, Buffers, TypedArray, or DataViews. If the cipher does not need an initialization vector, iv may be null.

    According to the documentation, the key and iv must both be either a UTF8 string, Buffer, TypeArray, or DataView. You may need to either change he key to a Buffer or the iv to a string.

    var cipher = crypto.createCipheriv('aes128-cbc', 
                                       Buffer.from(encryptionKey, 'utf8'),
                                       Buffer.from(iv, 'utf8'));