久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<i id='3JA6z'><tr id='3JA6z'><dt id='3JA6z'><q id='3JA6z'><span id='3JA6z'><b id='3JA6z'><form id='3JA6z'><ins id='3JA6z'></ins><ul id='3JA6z'></ul><sub id='3JA6z'></sub></form><legend id='3JA6z'></legend><bdo id='3JA6z'><pre id='3JA6z'><center id='3JA6z'></center></pre></bdo></b><th id='3JA6z'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='3JA6z'><tfoot id='3JA6z'></tfoot><dl id='3JA6z'><fieldset id='3JA6z'></fieldset></dl></div>
    • <bdo id='3JA6z'></bdo><ul id='3JA6z'></ul>

      <small id='3JA6z'></small><noframes id='3JA6z'>

      <legend id='3JA6z'><style id='3JA6z'><dir id='3JA6z'><q id='3JA6z'></q></dir></style></legend>

      1. <tfoot id='3JA6z'></tfoot>
      2. 在 .NET 中使用 AES 加密 - CryptographicException 表示填

        Using AES encryption in .NET - CryptographicException saying the padding is invalid and cannot be removed(在 .NET 中使用 AES 加密 - CryptographicException 表示填充無效且無法刪除)
        <tfoot id='npxet'></tfoot>

      3. <small id='npxet'></small><noframes id='npxet'>

            <bdo id='npxet'></bdo><ul id='npxet'></ul>
            • <legend id='npxet'><style id='npxet'><dir id='npxet'><q id='npxet'></q></dir></style></legend>

                  <tbody id='npxet'></tbody>
                1. <i id='npxet'><tr id='npxet'><dt id='npxet'><q id='npxet'><span id='npxet'><b id='npxet'><form id='npxet'><ins id='npxet'></ins><ul id='npxet'></ul><sub id='npxet'></sub></form><legend id='npxet'></legend><bdo id='npxet'><pre id='npxet'><center id='npxet'></center></pre></bdo></b><th id='npxet'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='npxet'><tfoot id='npxet'></tfoot><dl id='npxet'><fieldset id='npxet'></fieldset></dl></div>
                2. 本文介紹了在 .NET 中使用 AES 加密 - CryptographicException 表示填充無效且無法刪除的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我用 C# 編寫了一些 AES 加密代碼,但無法正確加密和解??密.如果我輸入test"作為密碼并且這個數(shù)據(jù)必須對所有人保密!"我收到以下異常:

                  I wrote some AES encryption code in C# and I am having trouble getting it to encrypt and decrypt properly. If I enter "test" as the passphrase and "This data must be kept secret from everyone!" I receive the following exception:

                  System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed.
                     at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
                     at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
                     at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
                     at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
                     at System.IO.Stream.Close()
                     at System.IO.Stream.Dispose()
                     ...
                  

                  如果我輸入的內(nèi)容少于 16 個字符,我不會得到任何輸出.

                  And if I enter something less than 16 characters I get no output.

                  我認為我需要在加密中進行一些特殊處理,因為 AES 是一種分組密碼,但我不確定那是什么,而且我無法在網(wǎng)絡上找到任何示例來說明如何操作.這是我的代碼:

                  I believe I need some special handling in the encryption since AES is a block cipher, but I'm not sure exactly what that is, and I wasn't able to find any examples on the web showing how. Here is my code:

                  using System;
                  using System.IO;
                  using System.Security.Cryptography;
                  using System.Text;
                  
                  public static class DatabaseCrypto
                  {
                      public static EncryptedData Encrypt(string password, string data)
                      {
                          return DatabaseCrypto.Transform(true, password, data, null, null) as EncryptedData;
                      }
                  
                      public static string Decrypt(string password, EncryptedData data)
                      {
                          return DatabaseCrypto.Transform(false, password, data.DataString, data.SaltString, data.MACString) as string;
                      }
                  
                      private static object Transform(bool encrypt, string password, string data, string saltString, string macString)
                      {
                          using (AesManaged aes = new AesManaged())
                          {
                              aes.Mode = CipherMode.CBC;
                              aes.Padding = PaddingMode.PKCS7;
                              int key_len = aes.KeySize / 8;
                              int iv_len = aes.BlockSize / 8;
                              const int salt_size = 8;
                              const int iterations = 8192;
                  
                              byte[] salt = encrypt ? new byte[salt_size] : Convert.FromBase64String(saltString);
                              if (encrypt)
                              {
                                  new RNGCryptoServiceProvider().GetBytes(salt);
                              }
                  
                              byte[] bc_key = new Rfc2898DeriveBytes("BLK" + password, salt, iterations).GetBytes(key_len);
                              byte[] iv = new Rfc2898DeriveBytes("IV" + password, salt, iterations).GetBytes(iv_len);
                              byte[] mac_key = new Rfc2898DeriveBytes("MAC" + password, salt, iterations).GetBytes(16);
                  
                              aes.Key = bc_key;
                              aes.IV = iv;
                  
                              byte[] rawData = encrypt ? Encoding.UTF8.GetBytes(data) : Convert.FromBase64String(data);
                  
                              using (ICryptoTransform transform = encrypt ? aes.CreateEncryptor() : aes.CreateDecryptor())
                              using (MemoryStream memoryStream = encrypt ? new MemoryStream() : new MemoryStream(rawData))
                              using (CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, encrypt ? CryptoStreamMode.Write : CryptoStreamMode.Read))
                              {
                                  if (encrypt)
                                  {
                                      cryptoStream.Write(rawData, 0, rawData.Length);
                  
                                      return new EncryptedData(salt, mac_key, memoryStream.ToArray());
                                  }
                                  else
                                  {
                                      byte[] originalData = new byte[rawData.Length];
                                      int count = cryptoStream.Read(originalData, 0, originalData.Length);
                  
                                      return Encoding.UTF8.GetString(originalData, 0, count);
                                  }
                              }
                          }
                      }
                  }
                  
                  public class EncryptedData
                  {
                      public EncryptedData()
                      {
                      }
                  
                      public EncryptedData(byte[] salt, byte[] mac, byte[] data)
                      {
                          this.Salt = salt;
                          this.MAC = mac;
                          this.Data = data;
                      }
                  
                      public EncryptedData(string salt, string mac, string data)
                      {
                          this.SaltString = salt;
                          this.MACString = mac;
                          this.DataString = data;
                      }
                  
                      public byte[] Salt
                      {
                          get;
                          set;
                      }
                  
                      public string SaltString
                      {
                          get { return Convert.ToBase64String(this.Salt); }
                          set { this.Salt = Convert.FromBase64String(value); }
                      }
                  
                      public byte[] MAC
                      {
                          get;
                          set;
                      }
                  
                      public string MACString
                      {
                          get { return Convert.ToBase64String(this.MAC); }
                          set { this.MAC = Convert.FromBase64String(value); }
                      }
                  
                      public byte[] Data
                      {
                          get;
                          set;
                      }
                  
                      public string DataString
                      {
                          get { return Convert.ToBase64String(this.Data); }
                          set { this.Data = Convert.FromBase64String(value); }
                      }
                  }
                  
                      static void ReadTest()
                      {
                          Console.WriteLine("Enter password: ");
                          string password = Console.ReadLine();
                  
                          using (StreamReader reader = new StreamReader("aes.cs.txt"))
                          {
                              EncryptedData enc = new EncryptedData();
                              enc.SaltString = reader.ReadLine();
                              enc.MACString = reader.ReadLine();
                              enc.DataString = reader.ReadLine();
                  
                              Console.WriteLine("The decrypted data was: " + DatabaseCrypto.Decrypt(password, enc));
                          }
                      }
                  
                      static void WriteTest()
                      {
                          Console.WriteLine("Enter data: ");
                          string data = Console.ReadLine();
                          Console.WriteLine("Enter password: ");
                          string password = Console.ReadLine();
                  
                          EncryptedData enc = DatabaseCrypto.Encrypt(password, data);
                  
                          using (StreamWriter stream = new StreamWriter("aes.cs.txt"))
                          {
                              stream.WriteLine(enc.SaltString);
                              stream.WriteLine(enc.MACString);
                              stream.WriteLine(enc.DataString);
                  
                              Console.WriteLine("The encrypted data was: " + enc.DataString);
                          }
                      }
                  

                  推薦答案

                  在需要填充的模式(如 CBC)中使用像 AES 這樣的分組密碼時,您必須注意輸出始終是分組大小的倍數(shù).為了實現(xiàn)這一點,像 PKCS7 這樣的填充模式將在加密過程結束時向密碼添加一些字節(jié).但是你必須讓加密器知道何時結束.為此,您所要做的就是插入語句

                  When using a block cipher like AES in a mode that requires padding, like CBC, you must be aware that the output will always be a multiple of the block size. To accomplish this, padding modes like PKCS7 will add some bytes to the cipher at the end of the encryption process. But you have to let the encryptor know when the end occurs. To do so, all you have to do is insert the statement

                  cryptoStream.FlushFinalBlock();  
                  

                  之后

                  cryptoStream.Write(rawData, 0, rawData.Length);
                  

                  PS:

                  也許它只是為了調(diào)試,但你的鹽生成方法每次都會生成完全相同的鹽.

                  Perhaps it is just for debugging, but your salt generation method generates the exact same salt every time.

                  這篇關于在 .NET 中使用 AES 加密 - CryptographicException 表示填充無效且無法刪除的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Deleting a directory when clicked on a hyperlink with JAvascript.ASP.NET C#(單擊帶有 JAvascript.ASP.NET C# 的超鏈接時刪除目錄)
                  asp.net listview highlight row on click(asp.net listview 在單擊時突出顯示行)
                  Calling A Button OnClick from a function(從函數(shù)調(diào)用按鈕 OnClick)
                  ASP.net C# Gridview ButtonField onclick event(ASP.net C# Gridview ButtonField onclick 事件)
                  Adding OnClick event to ASP.NET control(將 OnClick 事件添加到 ASP.NET 控件)
                  Multiple submit Button click problem?(多個提交按鈕點擊問題?)

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

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

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

                            <tfoot id='xtYVH'></tfoot>

                              <tbody id='xtYVH'></tbody>
                            <legend id='xtYVH'><style id='xtYVH'><dir id='xtYVH'><q id='xtYVH'></q></dir></style></legend>
                            主站蜘蛛池模板: 久久久蜜桃| 亚洲区一| 国产精品久久久久久久久久 | 色综合av | 久久久国产一区二区三区四区小说 | 7777久久 | 国产一区二区三区四区三区四 | 国产在线网站 | 亚洲欧美一区二区三区国产精品 | 成人在线中文字幕 | 国产高清免费视频 | 99国产精品99久久久久久 | 日本久草 | 91视频大全 | 最新av中文字幕 | 欧美一区二区三区高清视频 | 亚洲黄色一级毛片 | 久久一起草 | 国产综合区 | 成人影院av | 黄色网址在线免费观看 | 精品91久久 | 成人欧美一区二区三区黑人孕妇 | 在线看黄免费 | 国产成人免费网站 | 成年人网站免费视频 | 日韩精品一区二区三区在线观看 | 日韩免费一级 | 国产成在线观看免费视频 | 久久精品国产v日韩v亚洲 | 久久免费精品 | 国产精品视频久久久久 | 黄色成人免费在线观看 | 欧美精品在线一区二区三区 | 91精品久久久久久综合五月天 | 国产精品久久久久久亚洲调教 | 精品欧美一区二区中文字幕视频 | 91在线精品视频 | 日韩精品一区二区三区第95 | 欧美偷偷操 | 成人在线免费观看av |