Search code examples
c#aesasp.net-core-3.1

Matching Node.js aes-128-ecb (hex) encryption with C# .NET Core 3.1


I'm using an external API which expect the request body encrypted with aes-128-ecb (hex). I got it to work with JavaScript using Node.js with the following code (same key is used per session, but that's not the problem here):

    var crypto = require('crypto'),
        algorithm = 'aes-128-ecb',
        key = 'E572F45E8D79CAF92B4BD3B375820831';

    var fetchlist = {
        fetch_list: '[{"name":"itemInfo","controller":"catalog.BLCCatalogItem","method":"getDetailWithColor","params":[264,86]}]',
    };
    var message = JSON.stringify(fetchlist);

    var cipher = crypto.createCipher(algorithm, key);
    var crypted = cipher.update(message, 'utf8', 'hex');
    crypted += cipher.final('hex');

I want now to also do some requests from my C# .NET Core 3.1. application, but no matter what I try, I do not get the same result. The API is not accepting my request as well.

        var fetchList = new FetchList();
        fetchList.fetch_list = "[{\"name\":\"itemInfo\",\"controller\":\"catalog.BLCCatalogItem\",\"method\":\"getDetailWithColor\",\"params\":[264,86]}]";
        string message = JsonConvert.SerializeObject(fetchList);

        AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
        aes.BlockSize = 128;
        aes.KeySize = 128;
        aes.Key = UTF8Encoding.UTF8.GetBytes(Key);
        aes.Mode = CipherMode.ECB;
        aes.Padding = PaddingMode.PKCS7;
        byte[] data = Encoding.UTF8.GetBytes(message);

        ICryptoTransform encrypt = aes.CreateEncryptor();

        byte[] dest = encrypt.TransformFinalBlock(data, 0, data.Length);

        StringBuilder hex = new StringBuilder(dest.Length * 2);
        foreach (byte b in dest)
            hex.AppendFormat("{0:x2}", b);
        var output =  hex.ToString();

And FetchList Class:

public class FetchList
{
    public string fetch_list { get; set; }
}

Solution

  • Thanks to @Topaco I was able to solf my problem. here is how the code now looks like:

            var key = new OpenSslCompatDeriveBytes(UTF8Encoding.UTF8.GetBytes("E572F45E8D79CAF92B4BD3B375820831"), null, "MD5", 1).GetBytes(16);
    
            var fetchList = new FetchList();
            fetchList.fetch_list = "[{\"name\":\"itemInfo\",\"controller\":\"catalog.BLCCatalogItem\",\"method\":\"getDetailWithColor\",\"params\":[137696,115]}]";
            string message = JsonConvert.SerializeObject(fetchList);
    
            AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
            aes.BlockSize = 128;
            aes.KeySize = 128;
            aes.Key = key;
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;
            byte[] data = Encoding.UTF8.GetBytes(message);
    
            ICryptoTransform encrypt = aes.CreateEncryptor();
    
            byte[] dest = encrypt.TransformFinalBlock(data, 0, data.Length);
    
            StringBuilder hex = new StringBuilder(dest.Length * 2);
            foreach (byte b in dest)
                hex.AppendFormat("{0:x2}", b);
            var output =  hex.ToString();