C#到节点加密散列-md5和sha256

问题描述 投票:0回答:1

这是我要移植到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:

https://repl.it/@HarryLincoln/NegligiblePoisedHexagon

节点工作方式:

  • crypto.createCipheriv()确实看起来很不错,但是我不相信c#方法(SHA256.Create()和MD5.Create())关心密钥和iv的长度-但是[ C0]可以。

  • c#使用CryptoStream:所以我认为某种缓冲区是为了查看某些crypto.createCipheriv()

非常感谢您的帮助!

c# node.js aes md5 cryptojs
1个回答
0
投票

。Net Framework-AES加密默认使用256位密钥和CBC模式以及PKCS7填充。

要移植的代码很容易阅读,它只是这样做:

similar C# -> Node crypto stuff

可以在Node上轻松实现。

return

BASE64 (
    AES_ENCRYPT (
        password,
        Key: SHA256(username),
        IV: MD5(username)
   )
)
© www.soinside.com 2019 - 2024. All rights reserved.