gpt4 book ai didi

Nodejs 中的 Java 密码

转载 作者:行者123 更新时间:2023-11-29 05:47:03 25 4
gpt4 key购买 nike

我接到了这个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);

最佳答案

PBEWithMD5AndDES 是指加密算法。来自Java Cryptography Extension (JCE) Reference Guide :

PBEWithMD5AndDES: The password-based encryption algorithm as defined in: RSA Laboratories, "PKCS #5: Password-Based Encryption Standard," version 1.5, Nov 1993. Note that this algorithm implies CBC as the cipher mode and PKCS5Padding as the padding scheme and cannot be used with any other cipher modes or padding schemes.

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

关于Nodejs 中的 Java 密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15485409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com