2016-06-13 35 views
-2

我有這個c#代碼來加密數據,需要node.js中的解密方法。我嘗試了node.js的加密模塊,但無法繼續。需要解密方法在節點js下面c#加密方法

private string Encrypt(string clearText) 
{ 
    string EncryptionKey = "Something"; 
    byte[] clearBytes = Encoding.Unicode.GetBytes(clearText); 
    using (Aes encryptor = Aes.Create()) 
    { 
     Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); 
     encryptor.Key = pdb.GetBytes(32); 
     encryptor.IV = pdb.GetBytes(16); 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) 
      { 
       cs.Write(clearBytes, 0, clearBytes.Length); 
       cs.Close(); 
      } 
      clearText = Convert.ToBase64String(ms.ToArray()); 
     } 
    } 
    return clearText; 
} 

我已經在密碼模塊試圖

cryptogrphy.pbkdf2("Something", 'Ivan Medvedev', 1000, 128/4, function (cErr, cBuffer) { 
    var cKey = cBuffer.slice(0, 32); 
    var cIV = cBuffer.slice(0, 16); 
    var cDecipher = cryptogrphy.createDecipheriv("AES-128-CBC", cKey, cIV); 
    var sDecoded = cDecipher.update(text); 
    sDecoded += cDecipher.final("utf8"); 
    console.log(sDecoded); 
}); //'Ivan Medvedev' is equivalent string to salt byte in encryption method 
+0

如果需要,我可以提供C#編寫的 –

回答

0

有與您的Node.js代碼種種問題(AES-128與AES-256,IV是關鍵的組成部分,混合解密文本的編碼)。

cryptogrphy.pbkdf2("Something", 'Ivan Medvedev', 1000, 32+16, 'sha1', function (cErr, cBuffer) { 
    var cKey = cBuffer.slice(0, 32); 
    var cIV = cBuffer.slice(32); 
    var cDecipher = cryptogrphy.createDecipheriv("AES-256-CBC", cKey, cIV); 
    var sDecoded = cDecipher.update(text, 'binary', 'utf8'); 
    sDecoded += cDecipher.final("utf8"); 
    console.log(sDecoded); 
}); 
+0

解密方法目前還不清楚text'是什麼',所以我認爲這是一個Latin1的編碼的密文字符串。 –

+0

得到異常IV長度無效 –

+0

對不起。我自己混合了比特和字節 –