将Decryption从C#转换为NodeJS

时间:2016-01-30 15:05:35

标签: c# node.js encryption

我有以下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代码没有。对此事的任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:1)

这会为我返回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);