From:
 
https://crypto.stackexchange.com/questions/54017/can-we-calculate-aes-ciphertext-length-based-on-the-length-of-the-plaintext?newreg=1ce930f3fa664ef3a5c17da4d32462b4
I see: output_size = input_size + (16 - (input_size % 16))
 
But this does not seem to correspond with what I see when running tests using C# and the AesCng with PKCS7 padding and CBC CipherMode.
 
Thoughts on how I calculate the length of the encrypted value?
 
At the end of the code, I have the "actual" length (that seen when running the code) -vs- the theoretical (per the above equation) and it shows how they differ.
 
Here's the code for sample program:
  1. Public Shared Function Encrypt(ByVal plainText As String, ByVal aes256BitKeyValue As String, ByVal initialVector As String) As String    
  2. If String.IsNullOrEmpty(plainText) Then    
  3. Return String.Empty    
  4. End If    
  5. Dim initialVectorBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(initialVector)    
  6. Dim plainTextBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(plainText)    
  7. Dim cipherTextBytes As Byte() = Nothing    
  8. Using symmetricKey As New System.Security.Cryptography.AesCng()    
  9. symmetricKey.Padding = Security.Cryptography.PaddingMode.PKCS7    
  10. symmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC    
  11. Dim keyBytes As Byte() = StringToByteArray(aes256BitKeyValue)    
  12. Try    
  13. Using cryptographyTransform As System.Security.Cryptography.ICryptoTransform = symmetricKey.CreateEncryptor(keyBytes, initialVectorBytes)    
  14. Using memoryStream As New System.IO.MemoryStream()    
  15. Using cryptographyStream As New System.Security.Cryptography.CryptoStream(memoryStream, cryptographyTransform, System.Security.Cryptography.CryptoStreamMode.Write)    
  16. cryptographyStream.Write(plainTextBytes, 0, plainTextBytes.Length)    
  17. cryptographyStream.FlushFinalBlock()    
  18. cipherTextBytes = memoryStream.ToArray()    
  19. memoryStream.Close()    
  20. cryptographyStream.Close()    
  21. End Using    
  22. End Using    
  23. End Using    
  24. Catch ex As Exception    
  25. Throw New Exceptions.EncryptionException(String.Format(System.Globalization.CultureInfo.InvariantCulture, Resources.ExceptionMessages.EncryptionFailed1, ex))    
  26. Finally    
  27. symmetricKey.Clear()    
  28. End Try    
  29. End Using    
  30. Return Convert.ToBase64String(cipherTextBytes)    
  31. End Function 
  1.  
  2. namespace ConsoleApplication1  
  3. {  
  4. class Program  
  5. {  
  6. static void Main(string[] args)  
  7. {  
  8. string aes256Key = "1234567890123456789012345678901234567890123456789012345678901234";  
  9. string initialVector = "66sDeG*3xsS334dE";  
  10. string unencrypted20Text = "12345678901234567890";  
  11. string unencrypted40Text = "1234567890123456789012345678901234567890";  
  12. string unencrypted64Text = "1234567890123456789012345678901234567890123456789012345678901234";  
  13. string unencrypted128Text = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678";  
  14. string unencrypted256Text = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456";  
  15. string unencrypted257Text = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567";  
  16. string unencrypted512Text = unencrypted256Text + unencrypted256Text;  
  17. string unencrypted1024Text = unencrypted512Text + unencrypted512Text;  
  18. string unencrypted2048Text = unencrypted1024Text + unencrypted1024Text;  
  19. string unencrypted4096Text = unencrypted2048Text + unencrypted2048Text;  
  20. string unencrypted8192Text = unencrypted4096Text + unencrypted4096Text;  
  21. string encrypted20Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted20Text, aes256Key, initialVector);  
  22. string encrypted40Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted40Text, aes256Key, initialVector);  
  23. string encrypted64Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted64Text, aes256Key, initialVector);  
  24. string encrypted128Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted128Text, aes256Key, initialVector);  
  25. string encrypted256Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted256Text, aes256Key, initialVector);  
  26. string encrypted257Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted257Text, aes256Key, initialVector);  
  27. string encrypted512Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted512Text, aes256Key, initialVector);  
  28. string encrypted1024Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted1024Text, aes256Key, initialVector);  
  29. string encrypted2048Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted2048Text, aes256Key, initialVector);  
  30. string encrypted4096Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted4096Text, aes256Key, initialVector);  
  31. string encrypted8192Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted8192Text, aes256Key, initialVector);  
  32. int encrypted20TextLength = encrypted20Text.Length; // actual: 44, expected 20 + (16 - 4) = 32  
  33. int encrypted40TextLength = encrypted40Text.Length; // actual: 64, expected 40 + (116 - 8) = 48  
  34. int encrypted64TextLength = encrypted64Text.Length; // actual: 108, expected 64 + (16 - 0) = 80  
  35. int encrypted128TextLength = encrypted128Text.Length; // actual: 192, expected 128 + (16 - 0) = 144  
  36. int encrypted256TextLength = encrypted256Text.Length; // actual: 364  
  37. int encrypted257TextLength = encrypted257Text.Length; // actual: 364  
  38. int encrypted512TextLength = encrypted512Text.Length; // actual: 704  
  39. int encrypted1024TextLength = encrypted1024Text.Length; // actual: 1388  
  40. int encrypted2048TextLength = encrypted2048Text.Length; // actual: 2752  
  41. int encrypted4096TextLength = encrypted4096Text.Length; // actual: 5484  
  42. int encrypted8192TextLength = encrypted8192Text.Length; // actual: 10944  
  43. }  
  44. }  
  45. }