Aleksei Zabrodskii
Aleksei Zabrodskii

Reputation: 2228

Node.js: Wrong crypto output

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.

Upvotes: 1

Views: 821

Answers (1)

rossum
rossum

Reputation: 15693

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.

Upvotes: 2

Related Questions