Math 计算加密数据的最大大小

Math 计算加密数据的最大大小,math,encryption,aes,rijndaelmanaged,rijndael,Math,Encryption,Aes,Rijndaelmanaged,Rijndael,有没有办法计算固定数组长度的Rijndael加密的最大结果 加密方法:RijndaelManaged 填充:PKCS7 密码模式:CBC 块大小128 密钥大小:128 我需要这个,因为我正在转换一个所有字符串都将被加密的数据库,所以我需要更改所有字符串字段的大小。是。将输入大小四舍五入到块大小的最近倍数(例如128/8=16字节) Jeff的回答几乎是正确的,除了PKCS7始终会在消息中添加填充,即使消息正好位于整数块中。此外,不要忘记,如果使用随机静脉注射,也必须存储静脉注射。PKCS7填充

有没有办法计算固定数组长度的Rijndael加密的最大结果

加密方法:RijndaelManaged

填充:PKCS7

密码模式:CBC

块大小128

密钥大小:128


我需要这个,因为我正在转换一个所有字符串都将被加密的数据库,所以我需要更改所有字符串字段的大小。

是。将输入大小四舍五入到块大小的最近倍数(例如128/8=16字节)


Jeff的回答几乎是正确的,除了PKCS7始终会在消息中添加填充,即使消息正好位于整数块中。此外,不要忘记,如果使用随机静脉注射,也必须存储静脉注射。PKCS7填充消息长度的修正公式为:

extraBytesNeeded = (16 - (inputSize % 16)); // whole block of padding if input fits exactly
maxSize = inputSize + extraBytesNeeded + IVbytes;

您需要尝试的一切:


   public partial class Form1 : Form
   {
      private SymmetricAlgorithm mEncryptionType;

      public Form1()
      {
         mEncryptionType = new RijndaelManaged();
         mEncryptionType.Padding = PaddingMode.PKCS7; //PaddingMode.None;
         mEncryptionType.Mode = CipherMode.CBC;
         mEncryptionType.BlockSize = 128; // 192; // 256; // Update byte array to IV when changed
         mEncryptionType.KeySize = 128; // 192; // 256; // Update byte array to Key when changed
         mEncryptionType.IV = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                                           0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
         mEncryptionType.Key = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
                                           0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };

         int encrypted_size = CalculateEncryptedSize(new byte[] { 0x22, 0x23, 0x44 });
         // Shows Theran's point about exact block size
         encrypted_size = CalculateEncryptedSize(new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
                                           0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF  });
      }

      /// <summary>
      /// Calculate the encrypted size of input buffer
      /// </summary>
      /// <param name="inputBuffer">The input buffer</param>
      /// <returns>Size of the encrypted buffer</returns>
      public int CalculateEncryptedSize(byte[] inputBuffer)
      {
         int extra_padding = 0;
         if (mEncryptionType.Padding != PaddingMode.None)
         {
            int padding_size = (mEncryptionType.BlockSize / 8);
            extra_padding = (padding_size - (inputBuffer.Length % padding_size));
         }
         return inputBuffer.Length + extra_padding;
      }
   }

注意第二个等式,并不是每种语言都像你假设的那样处理负数的模。例如,在这些语言中,C/C++会给您错误的答案(-a%b)==-(a%b)。

   public partial class Form1 : Form
   {
      private SymmetricAlgorithm mEncryptionType;

      public Form1()
      {
         mEncryptionType = new RijndaelManaged();
         mEncryptionType.Padding = PaddingMode.PKCS7; //PaddingMode.None;
         mEncryptionType.Mode = CipherMode.CBC;
         mEncryptionType.BlockSize = 128; // 192; // 256; // Update byte array to IV when changed
         mEncryptionType.KeySize = 128; // 192; // 256; // Update byte array to Key when changed
         mEncryptionType.IV = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                                           0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
         mEncryptionType.Key = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
                                           0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };

         int encrypted_size = CalculateEncryptedSize(new byte[] { 0x22, 0x23, 0x44 });
         // Shows Theran's point about exact block size
         encrypted_size = CalculateEncryptedSize(new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
                                           0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF  });
      }

      /// <summary>
      /// Calculate the encrypted size of input buffer
      /// </summary>
      /// <param name="inputBuffer">The input buffer</param>
      /// <returns>Size of the encrypted buffer</returns>
      public int CalculateEncryptedSize(byte[] inputBuffer)
      {
         int extra_padding = 0;
         if (mEncryptionType.Padding != PaddingMode.None)
         {
            int padding_size = (mEncryptionType.BlockSize / 8);
            extra_padding = (padding_size - (inputBuffer.Length % padding_size));
         }
         return inputBuffer.Length + extra_padding;
      }
   }