Nodejs解密与java加密
Nodejs解密与java加密
我们公司用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中对应应该用哪个进行解密啊?求各路大神指导下。
当然可以!让我们来详细分析一下如何在 Node.js 中解密 Java 使用 DESede 加密的数据。
Java 端加密过程
Java 代码中使用了 DESede(Triple DES)算法进行加密。具体步骤如下:
- 获取一个
SecretKey
对象。 - 初始化
Cipher
实例用于加密模式。 - 执行加密操作。
final String Algorithm = "DESede";
byte[] encoded = null;
Cipher cipher = null;
SecretKey secretKey = null;
try {
secretKey = getSecretKey(enkey, Algorithm); // 假设 getSecretKey 是获取 SecretKey 的方法
cipher = Cipher.getInstance(Algorithm);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
encoded = cipher.doFinal(reqStr.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
Node.js 端解密过程
在 Node.js 中,我们可以使用 crypto
模块来实现 DESede 解密。以下是一个完整的示例代码:
1. 安装必要的依赖
首先确保你已经安装了 crypto
模块,这是 Node.js 自带的模块,不需要额外安装。
2. 编写解密代码
const crypto = require('crypto');
function decrypt(data, key) {
const algorithm = 'des-ede3-cbc'; // DESede-CBC
const iv = Buffer.alloc(8, 0); // 初始化向量,根据实际情况设置
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = decipher.update(data, 'binary', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
// 示例数据
const encryptedData = Buffer.from('your_encrypted_data_here', 'base64'); // 假设加密后的数据是 base64 编码的
const key = Buffer.from('your_secret_key_here', 'hex'); // 密钥也需要是正确的格式
const decryptedData = decrypt(encryptedData, key);
console.log(decryptedData);
关键点说明
- 算法选择:在 Node.js 中,
des-ede3-cbc
是 Triple DES(DESede)的正确算法名称。 - 密钥格式:确保密钥的格式和长度符合 Java 和 Node.js 的要求。通常密钥长度为 24 字节(192 位)。
- 初始化向量(IV):如果 Java 端使用了 IV,你需要在 Node.js 中提供相同的 IV。如果 Java 端没有使用 IV,则可以将 IV 设置为空或默认值。
注意事项
- 确保 Java 和 Node.js 使用相同的密钥和 IV。
- 如果 Java 端使用了不同的填充方式或其他参数,需要在 Node.js 中相应地调整。
通过以上步骤,你应该能够在 Node.js 中成功解密由 Java 使用 DESede 加密的数据。希望这对你有所帮助!
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加密出来始终不对。能指导下吗?
在Java中使用DESede
(三重DES)加密的数据,可以使用Node.js中的crypto
模块来进行解密。以下是一个示例代码,展示如何在Node.js中解密Java的DESede
加密数据。
Java 加密代码(已提供)
final String Algorithm = "DESede";
byte[] encoded = null;
Cipher cipher = null;
SecretKey secretKey = null;
try {
secretKey = getSecretKey(enkey, Algorithm); // 假设 getSecretKey 方法已经实现
cipher = Cipher.getInstance(Algorithm);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
encoded = cipher.doFinal(reqStr.getBytes("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
Node.js 解密代码
假设你已经有了Java加密后的encoded
字节数组,并且知道用于加密的密钥(例如enkey
)。
const crypto = require('crypto');
// 密钥和向量(IV)需要一致,这里假设密钥是16字节
const enkey = 'your-16-byte-key'; // 实际使用时替换为你的密钥
const iv = Buffer.alloc(8, 0); // DESede 的IV是8字节
function getSecretKey(key, algorithm) {
return crypto.createSecretKey(key, 'hex');
}
function decrypt(data, key, iv) {
const decipher = crypto.createDecipheriv('des-ede3-cbc', key, iv);
let decrypted = decipher.update(data, 'binary', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
// 假设你已经有了Java加密后的字节数组
const encryptedData = Buffer.from('your-encrypted-data-here', 'base64'); // 替换为实际的Base64编码字符串
const secretKey = getSecretKey(enkey, 'DESede');
const decryptedData = decrypt(encryptedData, secretKey, iv);
console.log(decryptedData);
解释
- 密钥:
DESede
加密需要一个16或24字节的密钥。 - IV:DESede的初始化向量(IV)是8字节。
- 算法:在Node.js中,使用
des-ede3-cbc
表示三重DES的CBC模式。 - 解密函数:
decrypt
函数使用crypto.createDecipheriv
创建解密器,并调用update
和final
方法来解密数据。
确保在Java和Node.js之间使用的密钥和IV完全一致,以保证解密成功。