-----------------先贴代码--------------------------- var crypto = require('crypto'); var data = "156156165152165156156"; console.log('Original cleartext: ' + data); var algorithm = 'aes-128-ecb'; var key = '78541561566'; var clearEncoding = 'utf8'; var iv = ""; //var cipherEncoding = 'hex'; //If the next line is uncommented, the final cleartext is wrong. var cipherEncoding = 'base64'; var cipher = crypto.createCipheriv(algorithm, key,iv);
var cipherChunks = [];
cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
cipherChunks.push(cipher.final(cipherEncoding));
console.log(cipherEncoding + ' ciphertext: ' + cipherChunks.join(''));
var decipher = crypto.createDecipheriv(algorithm, key,iv);
var plainChunks = [];
for (var i = 0;i < cipherChunks.length;i++) {
plainChunks.push(decipher.update(cipherChunks[i], cipherEncoding, clearEncoding));
}
plainChunks.push(decipher.final(clearEncoding));
console.log("UTF8 plaintext deciphered: " + plainChunks.join(''));
----------------------------end-----------------------------
按照上面的方式,做加密是可以过的
但是当我加上别人的AES加密配置后(要解密他们的),就不行了 (他们用的是C#)
-----------------别人的配置数据------------------------
加密方式 AES 运算模式 ECB 块大小 128(位) 填充模式 零字节组成的字符串 密钥向量 { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF } 密钥 89622015104709087435617163207900 编码 UTF8 密文传输编码 BASE64 样例 明文:123456 密文:0cfAwa9X9XRpr53SKjfiug== (改数据配置,我用C#代码校验过,是可以通过的,与样例匹配)
按照该配置处理,会出现以下三个问题 Key='89622015104709087435617163207900' IV=[0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF] 1、TypeError: Not a buffer at TypeError (native) at new Cipheriv (crypto.js:187:16) at Object.Cipheriv (crypto.js:185:12) at Object.exports.AESEncrypt (d:\Node Work\MemberConduct\Tools\AES_KeChuan.js:23:23) at d:\Node Work\MemberConduct\routes\index.js:20:23 at Layer.handle [as handle_request] (d:\Node Work\MemberConduct\node_modules\express\lib\router\layer.js:95:5) at next (d:\Node Work\MemberConduct\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (d:\Node Work\MemberConduct\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (d:\Node Work\MemberConduct\node_modules\express\lib\router\layer.js:95:5) at d:\Node Work\MemberConduct\node_modules\express\lib\router\index.js:277:22
当我将IV转成buffer后,会出现以下问题 2、 Invalid IV length Error: Invalid IV length at Error (native) at new Cipheriv (crypto.js:187:16) at Object.Cipheriv (crypto.js:185:12) at Object.exports.AESEncrypt (d:\Node Work\MemberConduct\Tools\AES_KeChuan.js:23:23) at d:\Node Work\MemberConduct\routes\index.js:20:23 at Layer.handle [as handle_request] (d:\Node Work\MemberConduct\node_modules\express\lib\router\layer.js:95:5) at next (d:\Node Work\MemberConduct\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (d:\Node Work\MemberConduct\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (d:\Node Work\MemberConduct\node_modules\express\lib\router\layer.js:95:5) at d:\Node Work\MemberConduct\node_modules\express\lib\router\index.js:277:22
尝试按照例子代码中的IV='',会出现第三个问题 3、Invalid key length
npm 上搜索过好几个aes库,但是与数据配置都不符合, 有用Node做过这方面的,麻烦指点一下,不甚感激