Java 3DES和nodejs 3DES加密结果不匹配
发布于 1 个月前 作者 thinkive 254 次浏览 来自 问答

大家好,困扰我很久了,您能帮我分析下吗? 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加密出来始终不配。大家能指导下吗?

5 回复

写成这样谁高兴读

代码比较乱,但是首先是你要清晰的了解3des基本加密的流程,然后解决思路应该是先检查你的输入数据,长度,编码格式,Padding的方式,向量iv的设置,具体的算法,确定是一致的吗? 自己不确定的话找个第三方的工具测试下,看下结果到底是java错了还是nodejs

@tomleader Java 没错,现在是要通过nodejs方式实现Java的加密过程

回到顶部