I got the task to convert this Java decryption method into Nodejs, but I don't really understand this Java stuff. I'm especially confused with PBEWithMD5AndDES. Please explain to me, how I can reproduce this decryption in 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;
}
I assume I have to do stuff similiar to this.
var inputStream = fs.createReadStream("file");
var decipher = crypto.createDecipher("des", "secret", new Buffer("0C9D4AE41E8315FC", "hex"));
inputStream.pipe(decipher).pipe(process.stdout);
PBEWithMD5AndDES refers to the encryption algorithm. From the 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.
You may need to write some code to do the actual decryption in node.js. Here is an implementation in ruby that may help you get started.