Question

Node.js strangely gives me wrong output when decrypting hex–encoded AES128 output.

function decrypt_data( data, key, iv )
{
    var dc = crypto.createDecipheriv( 'aes-128-cbc', hex_to_str(key), hex_to_str(iv) );
    var res = dc.update( data, 'hex', 'utf8' );
    res += dc.final( 'utf8' );

    console.log(res);

    return res;
}

This function seems completely okay, all tests are passed. But when I call it from real node server, it returns corrupted output.

Message's tail is ok, but first 32 symbols are complete garbage. Something like this:

�8���ro�&����AMD Accelerated Parallel Processing" : [ "Cayman", "Cayman", "AMD Phenom(tm) II X2 555 Processor" ] }, "request" : "hello", "version" : 1 }

No matter what input it takes, return–value is always wrong in first 32 bytes.

Was it helpful?

Solution

Your code is using CBC mode. In this mode corruption of the initial chunk of the output can often be caused by using the wrong IV. Check carefully that you are using exactly the same IV to encrypt and decrypt. That means checking it byte by byte.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top