Decrypting AES256 with node.js returns wrong final block length

前端 未结 3 1837
猫巷女王i
猫巷女王i 2020-11-27 06:45

Using this Gist I was able to successfully decrypt AES256 in Node.js 0.8.7. Then when I upgraded to Node.js 0.10.24, I now see this error:

TypeError:

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 07:10

    Ok, so there was a change to Crypto in the switch from 0.8 to 0.10 Crypto methods return Buffer objects by default, rather than binary-encoded strings

    This means the above code needs to specify encodings.

    These four lines:

    decoded = decipher.update(encryptdata);
    decoded += decipher.final();
    encryptdata = encipher.update(cleardata);
    encryptdata += encipher.final();
    

    Are changed to:

    decoded = decipher.update(encryptdata, 'binary', 'utf8');
    decoded += decipher.final('utf8');
    encryptdata = encipher.update(cleardata, 'utf8', 'binary');
    encryptdata += encipher.final('binary');
    

    This worked for me, but I am open to other suggestions.

提交回复
热议问题