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

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

        <tfoot id='YL9Rp'></tfoot>
      2. <legend id='YL9Rp'><style id='YL9Rp'><dir id='YL9Rp'><q id='YL9Rp'></q></dir></style></legend>

        如何在 .NET 中加密字符串?

        时间:2023-06-02

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

          <tbody id='FRZU6'></tbody>
        <tfoot id='FRZU6'></tfoot>

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

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

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

                • 本文介绍了如何在 .NET 中加密字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我必须加密/解密 Xml 文件中的一些敏感信息?是的,我可以通过编写自己的自定义算法来做到这一点.我想知道 .NET 中是否已经有内置方法可以做到这一点,以及我总是需要注意哪些方面..

                  I have to encrypt/decrypt some sensitive information in a Xml file? Yes I can do that by writing my own custom algorithms. I am wondering if there is already a built in way in .NET to do that and also what points I always need to take care..

                  推荐答案

                  下面是几个使用 .NET 框架加密和解密字符串的函数:

                  Here's a couple of functions that use the .NET framework to encrypt and decrypt a string:

                  public string EncryptString(string plainText)
                  {
                      // Instantiate a new RijndaelManaged object to perform string symmetric encryption
                      RijndaelManaged rijndaelCipher = new RijndaelManaged();
                  
                      // Set key and IV
                      rijndaelCipher.Key = Convert.FromBase64String("ABC");
                      rijndaelCipher.IV = Convert.FromBase64String("123");
                  
                      // Instantiate a new MemoryStream object to contain the encrypted bytes
                      MemoryStream memoryStream = new MemoryStream();
                  
                      // Instantiate a new encryptor from our RijndaelManaged object
                      ICryptoTransform rijndaelEncryptor = rijndaelCipher.CreateEncryptor();
                  
                      // Instantiate a new CryptoStream object to process the data and write it to the 
                      // memory stream
                      CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelEncryptor, CryptoStreamMode.Write);
                  
                      // Convert the plainText string into a byte array
                      byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);
                  
                      // Encrypt the input plaintext string
                      cryptoStream.Write(plainBytes, 0, plainBytes.Length);
                  
                      // Complete the encryption process
                      cryptoStream.FlushFinalBlock();
                  
                      // Convert the encrypted data from a MemoryStream to a byte array
                      byte[] cipherBytes = memoryStream.ToArray();
                  
                      // Close both the MemoryStream and the CryptoStream
                      memoryStream.Close();
                      cryptoStream.Close();
                  
                      // Convert the encrypted byte array to a base64 encoded string
                      string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
                  
                      // Return the encrypted data as a string
                      return cipherText;
                  }
                  
                  
                  public string DecryptString(string cipherText)
                  {
                      // Instantiate a new RijndaelManaged object to perform string symmetric encryption
                      RijndaelManaged rijndaelCipher = new RijndaelManaged();
                  
                      // Set key and IV
                      rijndaelCipher.Key = Convert.FromBase64String("ABC");
                      rijndaelCipher.IV = Convert.FromBase64String("123");
                  
                      // Instantiate a new MemoryStream object to contain the encrypted bytes
                      MemoryStream memoryStream = new MemoryStream();
                  
                      // Instantiate a new encryptor from our RijndaelManaged object
                      ICryptoTransform rijndaelDecryptor = rijndaelCipher.CreateDecryptor();
                  
                      // Instantiate a new CryptoStream object to process the data and write it to the 
                      // memory stream
                      CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelDecryptor, CryptoStreamMode.Write);
                  
                      // Will contain decrypted plaintext
                      string plainText = String.Empty;
                  
                      try
                      {
                          // Convert the ciphertext string into a byte array
                          byte[] cipherBytes = Convert.FromBase64String(cipherText);
                  
                          // Decrypt the input ciphertext string
                          cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
                  
                          // Complete the decryption process
                          cryptoStream.FlushFinalBlock();
                  
                          // Convert the decrypted data from a MemoryStream to a byte array
                          byte[] plainBytes = memoryStream.ToArray();
                  
                          // Convert the encrypted byte array to a base64 encoded string
                          plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);
                      }
                      finally
                      {
                          // Close both the MemoryStream and the CryptoStream
                          memoryStream.Close();
                          cryptoStream.Close();
                      }
                  
                      // Return the encrypted data as a string
                      return plainText;
                  }
                  

                  当然,我不建议像这样对密钥和初始化向量进行硬编码 :)

                  Of course I don't advise hardcoding the key and initialisation vector like this :)

                  这篇关于如何在 .NET 中加密字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 RSA 公钥解密使用 RSA 私钥加密的字符串 下一篇:.NET 加密许可证密钥?

                  相关文章

                  最新文章

                  <tfoot id='irbLX'></tfoot>

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

                • <small id='irbLX'></small><noframes id='irbLX'>

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