【问题标题】:Migrating encryption aes-256-ecb from node to .NET将加密 aes-256-ecb 从节点迁移到 .NET
【发布时间】:2019-06-20 10:56:22
【问题描述】:

我正在尝试将 node 中制作的加密代码迁移到 .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里面有更多选项,反正我都测试过了,没有结果不匹配。

【问题讨论】:

标签: .net node.js aes


【解决方案1】:

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

代码如下,我还创建了一个DotNet Fiddle: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);
    }
}

【讨论】:

    猜你喜欢
    • 2018-07-08
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    相关资源
    最近更新 更多