将加密的AES-256-ECB从节点迁移到.NET

时间:2019-06-20 10:56:22

标签: .net node.js aes

我正在尝试将节点中制作的加密代码迁移到.net。

这是节点:

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代码:

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

我认为问题在于自动填充。在node中有两个选项,false或true,但是在.NET中,有更多选项,无论如何我都测试了所有结果,没有结果不匹配。

1 个答案:

答案 0 :(得分:0)

我做了一些(小的)更改,它们都可以在Node.js和.NET中使用。

下面是代码,我还创建了一个DotNet小提琴:here

与Node.js代码相同:here

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

.NET

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