使用C#进行AES加密的密文长度是多少?

时间:2019-03-06 22:51:47

标签: encryption aes

发件人: Can we calculate AES ciphertext length based on the length of the plaintext?

我看到:output_size = input_size +(16-(input_size%16))

但是,这似乎与我使用C#和带有PKCS7填充和CBC CipherMode的AesCng运行测试时看到的不一致。

关于我如何计算加密值的长度的想法?

这是示例程序的代码:

Public Shared Function Encrypt(ByVal plainText As String, ByVal aes256BitKeyValue As String, ByVal initialVector As String) As String

  If String.IsNullOrEmpty(plainText) Then
    Return String.Empty
  End If

  Dim initialVectorBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(initialVector)
  Dim plainTextBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(plainText)
  Dim cipherTextBytes As Byte() = Nothing

  Using symmetricKey As New System.Security.Cryptography.AesCng()
    symmetricKey.Padding = Security.Cryptography.PaddingMode.PKCS7
    symmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC

    Dim keyBytes As Byte() = StringToByteArray(aes256BitKeyValue)

    Try

      Using cryptographyTransform As System.Security.Cryptography.ICryptoTransform = symmetricKey.CreateEncryptor(keyBytes, initialVectorBytes)
        Using memoryStream As New System.IO.MemoryStream()
          Using cryptographyStream As New System.Security.Cryptography.CryptoStream(memoryStream, cryptographyTransform, System.Security.Cryptography.CryptoStreamMode.Write)
            cryptographyStream.Write(plainTextBytes, 0, plainTextBytes.Length)
            cryptographyStream.FlushFinalBlock()
            cipherTextBytes = memoryStream.ToArray()
            memoryStream.Close()
            cryptographyStream.Close()
          End Using
        End Using
      End Using

    Catch ex As Exception
      Throw New Exceptions.EncryptionException(String.Format(System.Globalization.CultureInfo.InvariantCulture, Resources.ExceptionMessages.EncryptionFailed1, ex))

    Finally
      symmetricKey.Clear()
    End Try

  End Using

  Return Convert.ToBase64String(cipherTextBytes)

End Function

命名空间ConsoleApplication1 {   班级计划   {     静态void Main(string [] args)     {       字符串aes256Key =“ 1234567890123456789012345678901234567890123456789012345678901234”;       字符串initialVector =“ 66sDeG * 3xsS334dE”;

  string unencrypted20Text = "12345678901234567890";
  string unencrypted40Text = "1234567890123456789012345678901234567890";
  string unencrypted64Text = "1234567890123456789012345678901234567890123456789012345678901234";
  string unencrypted128Text = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678";
  string unencrypted256Text = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456";
  string unencrypted257Text = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567";
  string unencrypted512Text = unencrypted256Text + unencrypted256Text;
  string unencrypted1024Text = unencrypted512Text + unencrypted512Text;
  string unencrypted2048Text = unencrypted1024Text + unencrypted1024Text;
  string unencrypted4096Text = unencrypted2048Text + unencrypted2048Text;
  string unencrypted8192Text = unencrypted4096Text + unencrypted4096Text;

  string encrypted20Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted20Text, aes256Key, initialVector);
  string encrypted40Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted40Text, aes256Key, initialVector);
  string encrypted64Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted64Text, aes256Key, initialVector);
  string encrypted128Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted128Text, aes256Key, initialVector);
  string encrypted256Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted256Text, aes256Key, initialVector);
  string encrypted257Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted257Text, aes256Key, initialVector);
  string encrypted512Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted512Text, aes256Key, initialVector);
  string encrypted1024Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted1024Text, aes256Key, initialVector);
  string encrypted2048Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted2048Text, aes256Key, initialVector);
  string encrypted4096Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted4096Text, aes256Key, initialVector);
  string encrypted8192Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted8192Text, aes256Key, initialVector);

  int encrypted20TextLength = encrypted20Text.Length; // actual: 44, expected 20 + (16 - 4) = 32
  int encrypted40TextLength = encrypted40Text.Length; // actual: 64, expected 40 + (116 - 8) = 48
  int encrypted64TextLength = encrypted64Text.Length; // actual: 108, expected 64 + (16 - 0) = 80
  int encrypted128TextLength = encrypted128Text.Length; // actual: 192, expected 128 + (16 - 0) = 144
  int encrypted256TextLength = encrypted256Text.Length; // actual: 364 
  int encrypted257TextLength = encrypted257Text.Length; // actual: 364
  int encrypted512TextLength = encrypted512Text.Length; // actual: 704
  int encrypted1024TextLength = encrypted1024Text.Length; // actual: 1388
  int encrypted2048TextLength = encrypted2048Text.Length; // actual: 2752
  int encrypted4096TextLength = encrypted4096Text.Length; // actual: 5484
  int encrypted8192TextLength = encrypted8192Text.Length; // actual: 10944



}

} }

0 个答案:

没有答案