1

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);
2
  • Sidenote: I find it funny that the Nodejs equivalent is only about 5 LOC. Commented Mar 18, 2013 at 19:40
  • Yes, but how many LOC will it be once you get it working? :) Commented Mar 20, 2013 at 19:30

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So, I've added the PBEParameterSpec (which is always the same). I feel like the last step is to get the md5 thingy into the hat...
Hey @buschtoens were you able to get PBEWithMD5AndDES working in NodeJs. I have the same requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.