2016-01-30 30 views
0

我有以下C#加密方法,用於解密我的某個內部應用程序中的某些數據。將解密從C#轉換爲NodeJS

public string DecryptString(string Text) 
{ 
    bool flag = false; 
    if (string.IsNullOrEmpty(Text)) 
    { 
     return string.Empty; 
    } 
    if (!this.IsEncrypted(Text)) 
    { 
     return Text; 
    } 
    if ((Text.Length > 5) && (Text.Substring(0, 5) == "*UU__")) 
    { 
     Text = Text.Substring(5); 
     flag = true; 
    } 
    else 
    { 
     Text = Text.Substring(1, Text.Length - 1); 
    } 
    if (Text.Length < 2) 
    { 
     return Text; 
    } 
    TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider(); 
    MemoryStream stream = new MemoryStream(); 
    byte[] buffer = new byte[Text.Length/2]; 
    for (int i = 0; i < (Text.Length/2); i++) 
    { 
     int num = Convert.ToInt32(Text.Substring(i * 2, 2), 0x10); 
     buffer[i] = Convert.ToByte(num); 
    } 
    ICryptoTransform transform = provider.CreateDecryptor(this._DESKey, this._DESIV); 
    CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write); 
    stream2.Write(buffer, 0, buffer.Length); 
    stream2.FlushFinalBlock(); 
    byte[] bytes = stream.ToArray(); 
    StringBuilder builder = new StringBuilder(); 
    if (flag) 
    { 
     for (int j = 0; j < bytes.Length; j += 2) 
     { 
      builder.Append(Encoding.Unicode.GetChars(bytes, j, 2)); 
     } 
    } 
    else 
    { 
     foreach (byte num4 in bytes) 
     { 
      builder.Append(Convert.ToChar(num4)); 
     } 
    } 
    stream2.Close(); 
    return builder.ToString(); 
} 

我想將其轉換爲使用NodeJS。我曾嘗試各種方式來做到這一點首先使用CryptoJS,然後使用本機加密庫,但沒有運氣 下面是使用CryptoJS

var decrypt = function (word, key, iv, use_hashing) { 

    if (use_hashing) { 
     key = CryptoJS.MD5(key).toString(); 
     var k1 = key.substring(0, 16); 
     key = key + k1; 
    } 

    if (!word) { 
     return "pw not specified"; 
    } 
    /* 
    if(!is_encrypted(word)){ 
    return "pw is not encrypted"; 
    } 
    */ 

    //Remove * 
    word = word.substring(1); 
    console.log(word); 

    decrypt = CryptoJS.TripleDES.decrypt(
     word, 
     key, 
     {iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7}); 

    var tmp = decrypt.toString(CryptoJS.enc.Utf8); 
    return tmp 
}; 

var des_iv_str = 'd146ec4ce3f955cb' 
var des_key_str = 'dc5c3319dc25c1f6f11f6a792a6dd28864c9dd48be26c2e4' 
var crypto_hex_iv= CryptoJS.enc.Hex.parse(des_iv_str); 
var crypto_hex_key = CryptoJS.enc.Hex.parse(des_key_str); 

var decrypted_password = cypher.decrypt(encrypted_password, crypto_hex_key, crypto_hex_iv, false); 

console.log(decrypted_password); 

這個版本只返回一個空消息的例子。 下面是測試值

Example encrypted value is *6A57201D19B07ABFAE74B453BA46381C 
Example key in a decimal array is 220, 92, 51, 25, 220, 37, 193, 246, 241, 31, 106, 121, 42, 109, 210, 136, 100, 201, 221, 72, 190, 38, 194, 228 
Example key in string format is dc5c3319dc25c1f6f11f6a792a6dd28864c9dd48be26c2e4 
Example iv is 209, 70, 236, 76, 227, 249, 85, 203 
Example iv in string format is d146ec4ce3f955cb 
Example result is password 

的CS代碼工作正常,但代碼的NodeJS沒有。對此事的任何幫助將不勝感激。

+0

「的Unicode」 編碼在C#中可能是UTF-16LE,不與你擁有的NodeJS utf8的兼容。 –

回答

2

這對我返回password

var iv = new Buffer('d146ec4ce3f955cb', "hex"); 
var key = new Buffer('dc5c3319dc25c1f6f11f6a792a6dd28864c9dd48be26c2e4', "hex"); 
var encrypted = new Buffer('6A57201D19B07ABFAE74B453BA46381C', "hex"); 

var cipher = crypto.createDecipheriv('des3', key, iv); 
var result = cipher.update(encrypted); 
result += cipher.final(); 

console.log("result: " + result); 
+0

太棒了,就是這樣!非常感謝。 – thxmike