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
Expand all | Collapse all

Encryption and Decryption Using Crypto module

  • 1.  Encryption and Decryption Using Crypto module

    Posted Fri February 21, 2025 02:11 AM

    Hi Teams:

    I am using crypto module for encryption and decryption when i try to encrypt the payload using A256GCM  without authTag it encrypt the payload but when i try to decrypt the payload using the same mechanism its give an error Null 

     Encryption

    var crypto = require('crypto');
    var apim = require('apim' );
    
    var requestData = apim.getvariable('request.body' );
    var entityDetails = requestData.webServiceInputDetails.entityDetails
    console.error("[INFO] Received Request Data:", entityDetails);
    
    console.error("[INFO] Received Request Data:", requestData);
    
    11 - if (!requestData) {
    console.log("[ERROR] Request body is missing");
    context.message.body.write({ error: "Invalid request: body is missing" ));|
    return;
    }
    function generateAESKeyAndIV() {
    var key = crypto.randomBytes(32); // AES 256-bit key (32 bytes)
    var iv = crypto.randomBytes(12); // AES GCM mode needs 12-byte IV
    key = new Buffer(key,'base64')
    iv = new Buffer(iv,'base64')
    console.log("Generated AES Key (Base64):", key. toString('base64'));
    console.log("Generated IV (Base64):", iv.toString('base64'));
    return { key: key, iv: iv };
    function encryptWithAES(key, iv) {
    // var cipher = crypto.createCipheriv('aes256-cbc', key, iv);
    var cipher = crypto.createCipheriv('A256GCM' , key, iv);
    
    // Encrypt the plaintext data
    var encrypted = cipher.update(plaintextData);
    
    var finalData = cipher.final();
    console.log('encryptedData Data:', plaintextData);
    console.log('encrypted Data:', encrypted);
    
    console.log('finalData Data:', finalData);
    var finalDataWithbase64 = finalData. toString('base64' );
    
    console.log('Encrypted Data:', finalDataWithbase64);
    return {
    encryptedData: finalDataWithbase64,
    iv: iv.toString('base64')
    aes: key.toString('base64' ),
    
    };
    }
    var aesData = generateAESKeyAndIV();
    var crypto = require('crypto );
    var apim = require('apim');
    
    // ** Step 1 :** Get Encrypted Data from User Request
    var requestData = apim.getvariable('requestData'); // Parsed JSON object
    var aeskey = apim.getvariable('AESKey' ); // Parsed JSON object
    var iv = apim.getvariable('iv'); // Parsed JSON object
    
    console.error("[INFO] Received Encrypted Data:", requestData);
    
    try {
    // ** Step 2 :** Convert Base64 to Buffers
    var encryptedData = Buffer. from(requestData, 'base64');
    iv = Buffer.from(iv, 'base64');
    aesKey = Buffer.from(aesKey, 'base64');
    
    console.error("[INFO] Parsed IV:", iv.toString('hex'));
    console.error("[INFO] Parsed AES Key:", aesKey. toString('hex'));
    console. error("[INFO] Parsed Encrypted Data: ", encryptedData. toString( thow)
    // ** Step 3 :** Create Decipher
    console.error("Test")
    var decipher = crypto.createDecipheriv('A256GCM', aesKey, iv);
    
    console.error(" === ",decipher)
    
    // ** Step 4 :** Perform Decryption
    decipher.update(encryptedData);
    
    var originalPlainText = decipher. final();
    console.error("[INFO] originalPlainText:", originalPlainText);
    
    
    // Convert buffer to string (Assuming JSON)
    var decryptedText = originalPlainText.toString('utf-8');
    console.error("[INFO] Decrypted Data:", decryptedText);
    
    // // Parse JSON if applicable
    var parsedData;
    try {
    parsedData = JSON.parse(decryptedText);
    console.error("[INFO] Parsed Decrypted Data:", parsedData);
    } catch (jsonError) {
    console.error("[WARNING] Decrypted data is not valid JSON.");
    parsedData = decryptedText; // Return raw string if not JSON
    
    
    });
    
    // ** Step 5 :** Return Decrypted Data
    context.message.body.write({
    decryptedData: parsedData
    
    apim.output('application/json')
    


    ------------------------------
    Shah Wajahat
    ------------------------------


  • 2.  RE: Encryption and Decryption Using Crypto module

    Posted Mon February 24, 2025 09:36 AM

    Hi Team

    Is there any update?



    ------------------------------
    Shah Wajahat
    ------------------------------