我们公司用nodejs做了个项目,现在要嵌入到java做的大系统中去。其中需要涉及到java的加密和nodejs的解密。 java加密过程中:
//加密的方式
final String Algorithm = "DESede";
byte[] encoded = null;
Cipher cipher = null;
SecretKey secretKey = null;
try {
secretKey = getSecretKey(enkey,Algorithm);
cipher = Cipher.getInstance(Algorithm);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
encoded = cipher.doFinal(reqStr);
} catch (Exception e) {
}
上述中 DESede 在nodejs中对应应该用哪个进行解密啊?求各路大神指导下。
http://nodejs.org/api/crypto.html
你可以看看这个,虽然我也不清楚 DESede 对应哪个
根据: Java文档:http://docs.oracle.com/javase/7/docs/api/javax/crypto/KeyGenerator.html OpenSSL文档:http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT
DESede => 3DES
去 crypto 里面找到对应算法就好了
Every implementation of the Java platform is required to support the following standard KeyGenerator algorithms with the keysizes in parentheses:
- AES (128)
- DES (56)
- DESede (168)
- HmacSHA1
- HmacSHA256
引用自Java文档:http://docs.oracle.com/javase/7/docs/api/javax/crypto/KeyGenerator.html
3DES cipher suites using triple DES. DES cipher suites using DES (not triple DES).
OpenSSL文档:http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT
** 常识:DES的密匙长度是 56Bits 3DES的密匙长度是 168Bits **
谢谢各位
楼主 我有一个类似问题,困扰我很久了,您能帮我分析下吗? 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实现这套
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 getDesKey = function(norInfo){
var key = new Buffer(24);
for (var i = 0; i < 3; i++)
{
var str = getBytes(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;
}
function getBytes(str) {
var bytes = [], char; str = encodeURI(str);
while (str.length) { char = str.slice(0, 1); str = str.slice(1);
if ('%' !== char) {
bytes.push(char.charCodeAt(0));
} else {
char = str.slice(0, 2);
str = str.slice(2);
bytes.push(parseInt(char, 16));
}
}
return bytes; };
nodejs加密和java加密出来始终不对。能指导下吗?