开发者

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开发者_JAVA百科 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜