1

我得到了将这个 Java 解密方法转换为 Nodejs 的任务,但我不太了解这些 Java 东西。我特别困惑PBEWithMD5AndDES。请向我解释一下,如何在 Nodejs 中重现此解密。

private void readFile() {
  try {
    Cipher cipher = getCipher(2, "secret");
    DataInputStream dis;

    dis = new DataInputStream(new CipherInputStream(someInputStream, cipher));

    String field1 = dis.readUTF();
    String filed2 = dis.readUTF();
    dis.close();
  } catch (Exception e) { }
}

private Cipher getCipher(int mode, String password) throws Exception {
  Random random = new Random(43287234L);
  byte[] salt = new byte[8];
  random.nextBytes(salt);
  PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 5);

  SecretKey pbeKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(new PBEKeySpec(password.toCharArray()));
  Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
  cipher.init(mode, pbeKey, pbeParamSpec);
  return cipher;
}

我想我必须做与此类似的事情。

var inputStream = fs.createReadStream("file");
var decipher = crypto.createDecipher("des", "secret", new Buffer("0C9D4AE41E8315FC", "hex"));

inputStream.pipe(decipher).pipe(process.stdout);
4

1 回答 1

3

PBEWithMD5AndDES指加密算法。来自Java Cryptography Extension (JCE) 参考指南

PBEWithMD5AndDES:基于密码的加密算法,定义在:RSA 实验室,“PKCS #5: Password-Based Encryption Standard”,1.5 版,1993 年 11 月。请注意,此算法暗示 CBC 作为密码模式,PKCS5Padding 作为填充方案和不能与任何其他密码模式或填充方案一起使用。

您可能需要编写一些代码来在 node.js 中进行实际解密。这是ruby​​ 中的一个实现,可以帮助您入门。

于 2013-03-20T17:58:44.373 回答