'JAVA encrypted AES 256 CBC To NODE.JS - Problems to migrate code

I have been reading several similar questions and based on that for my initial code. Unfortunately, I still can't get it to work.

key="fb52042ada308dd1d4dfd8a3870d5ab5"
iv = "bb8e0b158f57f63dfeea86e24af1abfc"
data = {"MerchantId":"0000000000000001"}

Get SHA256 from "data" (dataSha256)

Get encryption from Encrypt dataSha256 + iv + key

Result of Hexa encryption, similar to: fd72fcc16b66d04cf0f4dd2265a59eb675103482bae806b405bb85595056f5770b3202b42d42a87b767892591a333eb6b5c9ad3ef34f4d415f8d3bbc3d0f389e2e5b6f7cd915520c7b2c19225680728b

When migrating the code from Java to Node.js, I don't get similar result. The first problem is that the "iv" should be 16 bytes, however in the code it is 32.

JAVA EXTRACT (original)

        public class AesEncryption implements SymmetricEncryptionComponent {
        Map<String, String> initParams;
        String key, iv;
        String mode, encoding;
        String keyFile;
        String ENCODING = "ISO-8859-1";
    
        public AesEncryption() {
        Security.addProvider(new BouncyCastleProvider());
        }
    
        // PARAMETERS INITIALITATION
        public void setInitParams()
        {
        initParams=params;
        key=” 1ea9a91b0ba908b44f598d2822499441”;
        iv= "f20946931dd6e8594dc6f469b5e583ab";
        mode= "AES/CBC/PKCS7Padding";
        encoding= "HEX";
        if(encoding.equalsIgnoreCase("BASE64")&&encoding.equalsIgnoreCase("HEX"))
        throw new IllegalArgumentException("AES.ENCODING can only be 'HEX' of 'BASE64'")
        }
    
        // INFORMATION CIPHERING return encodeBase24
        public String encrypt(String data) {
        byte[] output = null;
        try {
        byte[] keyBytes = decode(key);
        byte[] input = data.getBytes(ENCODING);
        AlgorithmParameterSpec ivSpec = new 
        IvParameterSpec(Hex.decodeHex(iv.toCharArray()));
        SecretKeySpec keySpec = null;
        keySpec = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance(mode);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
        output = cipher.doFinal(input);
        } catch (Exception e) {
        throw new EncryptionException("Error", e);
        }
        return encode(output);
        }
    
        // INFORMATION ENCODE
        private String encode(byte[] output) {
        if (mode.equalsIgnoreCase("BASE64"))
        return Base64.encodeBase64String(output);
        else
        return new String(Hex.encodeHex(output));
        }
    
        // INFORMATION DECODE
        private byte[] decode(String data) throws DecoderException {
        if (data.indexOf("=") > 0 || data.indexOf("+") > 0)
        return Base64.decodeBase64(data);
        else
        return Hex.decodeHex(data.toCharArray());
        }
        }

NODE EXTRACT (using crypto-js)

        const cryptojs = require("crypto-js");
        const crypto = require("crypto");
    
        let jsonData = {"MerchantId":"0000000000000001"};
        let key = 'fb52042ada308dd1d4dfd8a3870d5ab5';
        let iv = 'bb8e0b158f57f63dfeea86e24af1abfc';
        const jsonDataSha256 = 
        crypto.createHash('sha256').update(JSON.stringify(jsonData)).digest('hex');
    
        key = cryptojs.enc.Latin1.parse(key); //Convierte hex string -> word array
        iv = cryptojs.enc.Latin1.parse(iv);
        jsonDataSha256Bin = cryptojs.enc.Latin1.parse(jsonDataSha256);   //Convert hex 
        string -> word array
    
        console.log(key);
        console.log(iv);
        console.log(jsonDataSha256);
    
        let encrypted = cryptojs.AES.encrypt(jsonDataSha256Bin, key, {
        iv: iv,
        mode: cryptojs.mode.CBC,
        padding: cryptojs.pad.Pkcs7,
        });
    
        encrypted = encrypted.toString();
        const salida = crypto.createHash('sha256').update(encrypted).digest('hex');
    
        console.log(`${salida}`);
        //Equals to ce994c4d2b1f398ff0bed22c4b48e1f2170dbf26baf2eaacec96ea6b31667cd6
        //No match to Java output.

What will I be doing wrong? Any help is appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source