简体   繁体   中英

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.

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. 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

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. Any assistance in this matter would greatly appreciated.

This returns password for me:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM