.net - 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.
解决方案
我做了一些(小的)更改,并且在 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);
}
}
推荐阅读
- c# - unity3d 中的 Kafka 集成引发 Win32Exception 错误
- react-redux - React Router V4 - 组件不渲染
- api - 使用 Adsense API 在 AdMob 中访问广告系列报告
- function - 我怎样才能使这个功能更优雅
- google-sheets - ArrayFormula() 和 SumIf() 匹配 "This" Row()
- elasticsearch - 6.5.1 和 5.6.2 之间不稳定的跨集群搜索
- coq - Coq 中模块参数的特化
- amazon-web-services - 如何让 ec2 上的 nginx 与 https 一起工作
- sql - Oracle extractValue 返回无效令牌
- vue.js - Vue.js:在页面中多次包含相同的组件实例