문제

고정 어레이 길이의 Rijndael 암호화에서 가장 큰 결과를 계산할 수있는 방법이 있습니까?

암호화 방법 : Rijndaelmanaged

패딩 : PKCS7

Ciphermode : CBC

블록 크기 128

키 화 : 128

모든 문자열이 암호화되는 데이터베이스를 변환하는 IM이 필요하므로 모든 문자열 필드의 크기를 변경해야합니다.

도움이 되었습니까?

해결책

이것을 시도하는 데 필요한 모든 것 :


   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;
      }
   }

다른 팁

예. 입력 크기를 가장 가까운 배수 블록 크기로 반올림합니다 (예 : 128 / 8 = 16 바이트).

extraBytesNeeded = (16 - (inputSize % 16)) % 16;
maxSize = inputSize + extraBytesNeeded.

PKCS7은 메시지가 필수 블록 수에 정확히 맞더라도 항상 메시지에 패딩을 추가한다는 점을 제외하고는 Jeff의 답변이 거의 정확합니다. 또한 임의의 IV를 사용하는 경우 IV도 저장해야한다는 것을 잊지 마십시오. PKCS7 패딩 메시지의 길이에 대한 수정 된 공식은 다음과 같습니다.

extraBytesNeeded = (16 - (inputSize % 16)); // whole block of padding if input fits exactly
maxSize = inputSize + extraBytesNeeded + IVbytes;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top