[英]C# to node crypto hashing - md5 and sha256
Here is the C# code I'm trying to port into Node crypto, but since I don't know c# it's proving a little tricky!这是我试图移植到 Node crypto 中的 C# 代码,但由于我不知道 C#,所以证明它有点棘手!
public static string EncryptStringToBytes_Aes(string username, string password)
{
string encrypted = string.Empty;
byte[] clearBytes = Encoding.UTF8.GetBytes(password);
Console.WriteLine("1." + clearBytes);
using (Aes aesAlg = Aes.Create())
{
byte[] k; byte[] iv;
byte[] bytes = Encoding.UTF8.GetBytes(username);
k = SHA256.Create().ComputeHash(bytes);
iv = MD5.Create().ComputeHash(bytes);
aesAlg.Key = k;
aesAlg.IV = iv;
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) {
csEncrypt.Write(clearBytes, 0, clearBytes.Length); }
encrypted = Convert.ToBase64String(msEncrypt.ToArray());
}
}
return encrypted;
}
C# repl: C# 回复:
https://repl.it/@HarryLincoln/NegligiblePoisedHexagon https://repl.it/@HarryLincoln/NegligiblePoisedHexagon
Node workings:节点工作:
crypto.createCipheriv()
definitely looks like the way to go, but the I don't believe the c# methods (SHA256.Create() & MD5.Create()) care for the length of the key and iv - but crypto.createCipheriv()
does. crypto.createCipheriv()
看起来肯定是要走的路,但我不相信 c# 方法(SHA256.Create() 和 MD5.Create())关心密钥和 iv 的长度 - 但crypto.createCipheriv()
确实如此。
The c# uses a CryptoStream: So I think some kind of Buffer is in order looking at some similar C# -> Node crypto stuff C# 使用 CryptoStream:所以我认为某种 Buffer 是为了查看一些类似的 C# -> Node crypto 的东西
Would really appreciate some help!真的很感激一些帮助!
.Net Framework - AES encryption uses a 256 bit key and CBC mode and PKCS7 padding by default. .Net Framework - AES 加密默认使用 256 位密钥和 CBC 模式以及 PKCS7 填充。
The code to port is very simple to read, it just does this:移植的代码很容易阅读,它只是这样做:
return
BASE64 (
AES_ENCRYPT (
password,
Key: SHA256(username),
IV: MD5(username)
)
)
The same can easily be achieved on Node.同样可以在 Node.js 上轻松实现。
const crypto = require('crypto');
const key = crypto.createHash('sha256').update('username', 'utf8').digest();
const iv = crypto.createHash('md5').update('username', 'utf8').digest();
const encryptor = crypto.createCipheriv("aes-256-cbc", key, iv);
var crypted = Buffer.concat([encryptor.update('password', 'utf8'), encryptor.final()]);
let base64data = crypted.toString('base64');
console.log(base64data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.