大家好,困扰我很久了,您能帮我分析下吗? Java里面key是字节数组 KEY: private static byte[] getDesKey() { byte[] key = new byte[24]; MessageDigest digest; String norInfo = “asfasdadsf asdfasi12”; try { digest = MessageDigest.getInstance(“md5”); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage(), e); }
for (int i = 0; i < 3; i++)
{
byte[] buf = digest.digest(norInfo.substring(i).getBytes());
for (int j = 0; j < 8; j++)
{
key[(j + i * 8)] = buf[j];
}
}
return key;
}
加密 方法:SecretKey deskey = new SecretKeySpec(KEY, "DESede");
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(1, deskey);
return cipher.doFinal(source);
现在我需要用nodejs实现这套 key: var getDesKey = function(norInfo){ var key = new Buffer(24); for (var i = 0; i < 3; i++) { var str = new Buffer(norInfo.substring(i)); console.log(str); var buf = encryptMd51(str); console.log(buf) for (var j = 0; j < 8; j++) {
key[(j + i * 8)] = buf[j]; } }
return key;
}
var encrypt3Des = function(data, key, iv) {
var keys = getDesKey(key);
var iv = new Buffer(iv ? iv : 0);
var plaintext = data;
var alg = 'des-ede3';
var autoPad = true;
//encrypt
var cipher = crypto.createCipheriv(alg, key, iv);
cipher.setAutoPadding(autoPad); //default true
var ciph = cipher.update(plaintext, 'utf8', 'hex');
ciph += cipher.final('hex');
return (new Buffer(ciph).toString("base64"));
var encryptMd51 = function(data) { var buf = new Buffer(data); //var str = buf.toString(“binary”); return crypto.createHash(“md5”).update(buf).digest(); } nodejs加密和java加密出来始终不配。大家能指导下吗?