代码是基于nodejs写的,请到这个目录checkout出来,如果不checkout也行,依赖一个库,crypto-js
https://git.oschina.net/p2227...
主要代码如下
'use strict';
//common
var keyStr = 'D07D6882A1E5ADB5F65544C9';
var clearText = '634\0\0\0';
//----------------- program 1
var crypto = require('crypto');
var keyBuf = Buffer.from(keyStr);
var ivBuf = Buffer.alloc(0);
var cipherNode = crypto.createCipheriv('des-ede3',keyBuf,ivBuf);
cipherNode.update(clearText, 'utf8', 'hex');
var strNode = cipherNode.final('hex');
console.log(strNode); //9e7ab27a48fb1085
//----------------- program 2
var CryptoJS = require('crypto-js');
var keyjs = CryptoJS.enc.Latin1.parse(keyStr);
var options = {
mode: CryptoJS.mode.ECB,
iv:CryptoJS.enc.Latin1.parse(''),
padding: CryptoJS.pad.ZeroPadding
};
var strCryptojs = CryptoJS.TripleDES.encrypt(clearText, keyjs, options).ciphertext.toString();
console.log(strCryptojs); //d33b6a74ae3bef87
//-----decrypt
var decryptNodeByJs = CryptoJS.TripleDES.decrypt({
ciphertext: CryptoJS.enc.Hex.parse(strNode)
}, keyjs, options).toString(CryptoJS.enc.Utf8).replace(/[\u0000-\u0008]/g, '');
console.log(decryptNodeByJs);
// var decipherNode = crypto.createDecipheriv('des-ede3',keyBuf,ivBuf);
// decipherNode.update(strCryptojs);
// var decryptJSByNode = decipherNode.final('latin1'); //error
// console.log(decryptJSByNode);
主要疑问是,用node原生API加密的结果和crypto-js的结果不一样,但是后者能解密前者。
有没人能说清其中的原因?或者crypto-js的参数怎么调整能和原生API结果一样?
认证高级PHP讲师