使用的是CBC模式 现在遇到问题,C#的加密与Node的加密对不上 查了C#的代码,发现是用的PKCS7Padding 填充 初学Node,请教一下Node中的填充如何修改填充模式
代码如下 var _outputEncoding = ‘binary’, _inputEncoding = ‘utf8’, _encoding = ‘base64’, _module = ‘aes-256-cbc’;
exports.encrypt = function (data, key, iv) { var cipher = crypto.createCipheriv(_module, key, iv); cipher.setAutoPadding(true);//自动填充 var crypted = cipher.update(data, _inputEncoding, _outputEncoding); crypted += cipher.final(_outputEncoding); crypted = new Buffer(crypted, _outputEncoding).toString(_encoding); return crypted; }; exports.decrypt = function (crypted, key, iv) { crypted = new Buffer(crypted, _encoding).toString(_outputEncoding); var decipher = crypto.createDecipheriv(_module, key, iv); var decoded = decipher.update(crypted, _outputEncoding, _inputEncoding); decoded += decipher.final(_inputEncoding); return decoded; }; 代码 End