简体   繁体   English

将解密从C#转换为NodeJS

[英]Convert Decryption from C# to NodeJS

I have the following C# encryption method that is used to decrypt certain data on one of my internal apps. 我有以下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();
 }

I am trying to convert this to using NodeJS. 我正在尝试将其转换为使用NodeJS。 I have attempted various ways to do this first using CryptoJS and then using the native crypto libraries but no luck Here is an example of using CryptoJS 我尝试了各种方法,首先使用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);

This version only returns a blank message. 此版本仅返回空白消息。 Here are the test values 这是测试值

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

The CS code works fine but the NodeJS code does not. CS代码可以正常工作,但NodeJS代码却不能。 Any assistance in this matter would greatly appreciated. 在这方面的任何协助将不胜感激。

This returns password for me: 这将为我返回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