2013-03-18 84 views
1

我得到了將此Java解密方法轉換爲Nodejs的任務,但我並不真正瞭解這個Java的東西。我特別與PBEWithMD5AndDES混淆。請向我解釋一下,我如何在Nodejs中重現這個解密。Nodejs中的Java密碼

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); 
+0

旁註:我覺得有趣的是Nodejs等價物只有5 LOC左右。 – buschtoens 2013-03-18 19:40:07

+0

是的,但是一旦你使用它就會有多少LOC? :) – 2013-03-20 19:30:22

回答

3

PBEWithMD5AndDES指的是加密算法。從Java Cryptography Extension (JCE) Reference Guide

PBEWithMD5AndDES:基於密碼的加密算法中定義:RSA實驗室,「PKCS#5:基於密碼的加密標準,」 1.5版11月1993年請注意,這種算法意味着CBC作爲密碼模式並且PKCS5Padding作爲填充方案,並且不能與任何其他密碼模式或填充方案一起使用。

您可能需要編寫一些代碼來執行node.js中的實際解密。這裏有一個implementation in ruby,可以幫助你開始。

+0

感謝您的鏈接!我會盡力讓它工作。 :) – buschtoens 2013-03-20 23:21:59

+0

所以,我添加了'PBEParameterSpec'(它總是相同的)。我覺得最後一步是把md5變成帽子...... – buschtoens 2013-03-21 00:06:34

+0

嗨@buschtoens你可以在NodeJs中獲得PBEWithMD5AndDES。我有同樣的要求。 – DShah 2015-06-24 12:15:24