首页 > 解决方案 > Migrating encryption aes-256-ecb from node to .NET

问题描述

I am trying to migrate encryption code made in node to .net.

This is node:

function GenerateTokenByLit(literal) {
    const text_encrypt = Buffer.from(literal, 'utf8');
    const cipher = crypto.createCipheriv('aes-256-ecb', Buffer.from(SECRET_KEY, 'utf8'), '');
    cipher.setAutoPadding(true);
    const text = cipher.update(text_encrypt, 'buffer', 'base64');

    return text + cipher.final('base64');

}

.NET code:

public static string Encrypt(string original)
{
    byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    byte[] KEY = Encoding.UTF8.GetBytes(SECRET_KEY); 
    byte[] encrypted;
    using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
    {
        aesAlg.Padding = PaddingMode.None;
        aesAlg.Mode = CipherMode.ECB;
        aesAlg.KeySize = 256;

        // Create the streams used for encryption.
        //const cipher = crypto.createCipheriv('aes-256-ecb', Buffer.from(constants.API_ENVRYPTED_KEY, 'utf8'), '');
        //cipher.setAutoPadding(true);
        using (ICryptoTransform encryptor = aesAlg.CreateEncryptor(KEY, iv))
        using (MemoryStream msEncrypt = new MemoryStream())
        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
        {
            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
            {
                //Write all data to the stream.
                var bytes = Encoding.ASCII.GetBytes(original);
                swEncrypt.Write(bytes);
            }
            encrypted = msEncrypt.ToArray();
        }
    }
    return System.Convert.ToBase64String(encrypted);
}

I think that the problem is on autopadding. In node there is two options, false or true, but in .NET there are more options, anyway I tested all, and no the result does not match.

标签: .netnode.jsaes

解决方案


我做了一些(小的)更改,并且在 Node.js 和 .NET 中都可以正常工作。

代码如下,我还创建了一个 DotNet Fiddle:here

Node.js 代码也是如此:这里

节点.js

// See https://stackoverflow.com/questions/56684494/migrating-encryption-aes-256-ecb-from-node-to-net
const crypto = require("crypto");

// Change this if you're using for real !! 
const SECRET_KEY = Buffer.from("abcdefghijklmnopqrstuvwxyzabcdef", "utf8");

function GenerateTokenByLit(literal) {
    const text_encrypt = Buffer.from(literal, 'utf8');
    const cipher = crypto.createCipheriv('aes-256-ecb', SECRET_KEY, '');
    cipher.setAutoPadding(true);
    const text = cipher.update(text_encrypt, 'buffer', 'base64');
    return text + cipher.final('base64');
}

const plainText = "She's comin' on boys and she's comin' on strong.";
console.log("Plaintext: " + plainText);
console.log("Ciphertext: " + GenerateTokenByLit(plainText));

。网

using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;

public class Program
{
    public static readonly string SECRET_KEY = "abcdefghijklmnopqrstuvwxyzabcdef"; 

    public static void Main()
    {
        string plainText = "She's comin' on boys and she's comin' on strong.";
        Console.WriteLine("Plaintext: " + plainText);
        Console.WriteLine("Ciphertext: " + Encrypt(plainText) );
    }

    public static string Encrypt(string plainText)
    {
        byte[] KEY = Encoding.UTF8.GetBytes(SECRET_KEY); 
        byte[] encrypted;
        using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
        {
            aesAlg.Padding = PaddingMode.PKCS7;
            aesAlg.Mode = CipherMode.ECB;
            aesAlg.KeySize = 256;

            ICryptoTransform encryptor = aesAlg.CreateEncryptor(KEY, null);

            using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {

                            //Write all data to the stream.
                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
        }
        return System.Convert.ToBase64String(encrypted);
    }
}

推荐阅读