簡體   English   中英

將解密從C#轉換為NodeJS

[英]Convert Decryption from C# to NodeJS

我有以下C#加密方法,用於對我的一個內部應用程序中的某些數據進行解密。

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代碼卻不能。 在這方面的任何協助將不勝感激。

這將為我返回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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM