<bdo id='oBzQC'></bdo><ul id='oBzQC'></ul>

    <legend id='oBzQC'><style id='oBzQC'><dir id='oBzQC'><q id='oBzQC'></q></dir></style></legend>

  1. <i id='oBzQC'><tr id='oBzQC'><dt id='oBzQC'><q id='oBzQC'><span id='oBzQC'><b id='oBzQC'><form id='oBzQC'><ins id='oBzQC'></ins><ul id='oBzQC'></ul><sub id='oBzQC'></sub></form><legend id='oBzQC'></legend><bdo id='oBzQC'><pre id='oBzQC'><center id='oBzQC'></center></pre></bdo></b><th id='oBzQC'></th></span></q></dt></tr></i><div id='oBzQC'><tfoot id='oBzQC'></tfoot><dl id='oBzQC'><fieldset id='oBzQC'></fieldset></dl></div>
    <tfoot id='oBzQC'></tfoot>

      <small id='oBzQC'></small><noframes id='oBzQC'>

    1. 如何使用“System.Security.Cryptography.AesManaged"加

      时间:2023-06-02
      <tfoot id='eiA0V'></tfoot>

        <bdo id='eiA0V'></bdo><ul id='eiA0V'></ul>
        • <i id='eiA0V'><tr id='eiA0V'><dt id='eiA0V'><q id='eiA0V'><span id='eiA0V'><b id='eiA0V'><form id='eiA0V'><ins id='eiA0V'></ins><ul id='eiA0V'></ul><sub id='eiA0V'></sub></form><legend id='eiA0V'></legend><bdo id='eiA0V'><pre id='eiA0V'><center id='eiA0V'></center></pre></bdo></b><th id='eiA0V'></th></span></q></dt></tr></i><div id='eiA0V'><tfoot id='eiA0V'></tfoot><dl id='eiA0V'><fieldset id='eiA0V'></fieldset></dl></div>
            <tbody id='eiA0V'></tbody>
          <legend id='eiA0V'><style id='eiA0V'><dir id='eiA0V'><q id='eiA0V'></q></dir></style></legend>

                <small id='eiA0V'></small><noframes id='eiA0V'>

                本文介绍了如何使用“System.Security.Cryptography.AesManaged"加密一个字节[]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                基本上我想使用 System.Security.Cryptography.AesManaged (或者更好的类,如果你认为有的话?)获取一个字节数组并使用给定的对称密钥创建另一个加密字节数组(我假设我需要一个吗?).

                Basically i want to use System.Security.Cryptography.AesManaged (or a better class, if you think there is one?) to take one byte array and create another encrypted byte array, using a given symmetric key (i assume i'll need one?).

                我还需要逆向这个过程的方法.

                I also will need the way to reverse this procedure.

                这样做的目的是让我可以加密存储的密码.我认为有一种简单的方法可以做到这一点?

                The point of this is so i can encrypt stored passwords. I assume there's a simple way to do this?

                谢谢

                推荐答案

                你真的应该在每次加密时生成一个随机 IV,不像我下面的古老代码:

                You really should generate a random IV each time you encrypt, unlike my ancient code below:

                这是我最后所做的,灵感来自(旧版)迈克尔的回答:

                Here's what i did in the end, inspired by (an older version of) michael's answer:

                private string Encrypt(string input)
                {
                  return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(input)));
                }
                private byte[] Encrypt(byte[] input)
                {
                  PasswordDeriveBytes pdb = new PasswordDeriveBytes("hjiweykaksd", new byte[] { 0x43, 0x87, 0x23, 0x72, 0x45, 0x56, 0x68, 0x14, 0x62, 0x84 });
                  MemoryStream ms = new MemoryStream();
                  Aes aes = new AesManaged();
                  aes.Key = pdb.GetBytes(aes.KeySize / 8);
                  aes.IV = pdb.GetBytes(aes.BlockSize / 8);
                  CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write);
                  cs.Write(input, 0, input.Length);
                  cs.Close();
                  return ms.ToArray();
                }
                private string Decrypt(string input)
                {
                  return Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(input)));
                }
                private byte[] Decrypt(byte[] input)
                {
                  PasswordDeriveBytes pdb = new PasswordDeriveBytes("hjiweykaksd", new byte[] { 0x43, 0x87, 0x23, 0x72, 0x45, 0x56, 0x68, 0x14, 0x62, 0x84 });
                  MemoryStream ms = new MemoryStream();
                  Aes aes = new AesManaged();
                  aes.Key = pdb.GetBytes(aes.KeySize / 8);
                  aes.IV = pdb.GetBytes(aes.BlockSize / 8);
                  CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write);
                  cs.Write(input, 0, input.Length);
                  cs.Close();
                  return ms.ToArray();
                }
                

                这篇关于如何使用“System.Security.Cryptography.AesManaged"加密一个字节[]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:C# 从文本文件中的公钥获取 CngKey 对象 下一篇:.NET 中的 ECDiffieHellmanCng 是否具有实现 NIST SP 800

                相关文章

                最新文章

              1. <small id='LZyHJ'></small><noframes id='LZyHJ'>

                  <i id='LZyHJ'><tr id='LZyHJ'><dt id='LZyHJ'><q id='LZyHJ'><span id='LZyHJ'><b id='LZyHJ'><form id='LZyHJ'><ins id='LZyHJ'></ins><ul id='LZyHJ'></ul><sub id='LZyHJ'></sub></form><legend id='LZyHJ'></legend><bdo id='LZyHJ'><pre id='LZyHJ'><center id='LZyHJ'></center></pre></bdo></b><th id='LZyHJ'></th></span></q></dt></tr></i><div id='LZyHJ'><tfoot id='LZyHJ'></tfoot><dl id='LZyHJ'><fieldset id='LZyHJ'></fieldset></dl></div>
                    <bdo id='LZyHJ'></bdo><ul id='LZyHJ'></ul>
                  1. <legend id='LZyHJ'><style id='LZyHJ'><dir id='LZyHJ'><q id='LZyHJ'></q></dir></style></legend>
                    <tfoot id='LZyHJ'></tfoot>