使用 C# .NET Core 3.1 匹配 Node.js aes-128-ecb(十六进制)加密

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

我正在使用外部 API,它希望请求正文使用 aes-128-ecb(十六进制)加密。我通过以下代码使用 Node.js 使其与 JavaScript 一起工作(每个会话使用相同的密钥,但这不是这里的问题):

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

我现在还想从我的 C# .NET Core 3.1 执行一些请求。应用程序,但无论我尝试什么,我都不会得到相同的结果。 API 也不接受我的请求。

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

和 FetchList Class:

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

感谢@Topaco,我能够解决我的问题。这是代码现在的样子:

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