API Connect

API Connect

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  AES128-cbc Encryption Decryption Issue

    Posted Wed May 31, 2023 02:18 AM
      |   view attached

    HI @Steve Linn ,

    I am doing encryption decryption using gateway script but getting below error while executing attached js file. Can you please help me in resolving this

    20230531T055510.228Z [0x85800020][crypto][error] apigw(apiconnect): tid(18321)[request][172.18.0.1] gtid(811f13326476e13e00004791): Incorrect number of arguments provided to function 'update()'. Expected number of arguments: 1; actual number of arguments: 3
    20230531T055510.228Z [0x85800007][gatewayscript][error] apigw(apiconnect): tid(18321)[request][172.18.0.1] gtid(811f13326476e13e00004791): GatewayScript processing Error 'SyntaxError: Incorrect number of arguments provided to function 'update()'. Expected number of arguments: 1; actual number of arguments: 3
    In file 'gatewayscript:///modules/_dputil.js' line:21, stack:SyntaxError: Incorrect number of arguments provided to function 'update()'. Expected number of arguments: 1; actual number of arguments: 3
        at Object._checkNumberOfArguments [as checkLength] (gatewayscript:///modules/_dputil.js:21:15)
        at drJSDecipher.Decipher.update (gatewayscript:///modules/crypto.js:626:15)
        at decrypt (local:///js/localtest_sandbox_encryptiondecryption_1.0.0_gatewayscript_6.js:17:11)
        at Object.<anonymous> (local:///js/localtest_sandbox_encryptiondecryption_1.0.0_gatewayscript_6.js:30:23)
        at Script.execute (gatewayscript:///datapower.js:155:24)
        at Object.<anonymous> (gatewayscript:///datapower.js:582:55)'


    ------------------------------
    Fahad Hassan
    ------------------------------

    Attachment(s)

    txt
    js.txt   1 KB 1 version


  • 2.  RE: AES128-cbc Encryption Decryption Issue

    Posted Wed May 31, 2023 09:00 AM
    Edited by Tom van Oppens Wed May 31, 2023 09:00 AM

    couple of small errors in your script
    - you didn't define the output of the encryption, hence it would be a buffer
    - you did add an extra parameter in the decipher update (that's the error it's complaining about above)
    - you didn't define the decipher final 

    The following script is the working version

    const crypto = require('crypto');
    
    // Encryption
    function encrypt(plainText, encryptionKey, iv) {
      const cipher = crypto.createCipheriv('aes128-cbc', encryptionKey, iv);
      let encrypted = cipher.update(plainText);
      return cipher.final('hex');;
    }
    
    // Decryption
    function decrypt(encryptedText, encryptionKey, iv) {
        console.error('Insidie Decrypt:', encryptedText );
        console.error('Insidie Decrypt iv:', iv );
    
      var decipher = crypto.createDecipheriv('aes128-cbc', encryptionKey, iv);
     decipher.update(encryptedText,'hex');
     var originalPlainText = decipher.final('utf8');
      return originalPlainText;
    }
    
    // Example usage
    const encryptionKey = Buffer.from('0123456789abcdef0123456789abcdef','hex');
    const iv = Buffer.from(crypto.randomBytes(16),'hex');
    const plainText = 'This is a test message';
    
    const encryptedText = encrypt(plainText, encryptionKey, iv);
    console.error('Encrypted text:', encryptedText);
    
    const decryptedText = decrypt(encryptedText, encryptionKey, iv);
    console.error('Decrypted text:', decryptedText);


    ------------------------------
    Tom van Oppens
    ------------------------------