Bug 14243 - Error "Invalid input block size" using AES Encryption
Summary: Error "Invalid input block size" using AES Encryption
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 6.9.5.x
Hardware: Other Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2013-08-26 12:26 UTC by Jason
Modified: 2017-11-20 12:06 UTC (History)
5 users (show)

Tags: AES AESManged
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Jason 2013-08-26 12:26:18 UTC
I created a library to use between both Xamarin.Android and Xamarin.Ios that uses AES encryption to decrypt strings.  While the Android version works fine, using the exact same code, I get an error with the Xamarin.Ios version of the code.

The function looks like this

        public string DecryptStr(byte[] encryptedString)
            string ret = "";

            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = new AesManaged().CreateDecryptor(m_key, m_iv);

            // Create the streams used for decryption. 
            MemoryStream msDecrypt = new MemoryStream(encryptedString);
            CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
            StreamReader srDecrypt = new StreamReader(csDecrypt);

            // Read the decrypted bytes from the decrypting stream 
            // and place them in a string.
                ret = srDecrypt.ReadLine();
            catch (Exception ex)
                ;// this is where the error is thrown
            return ret;
Comment 1 Jason 2013-08-26 12:28:20 UTC
Stack Trace
ex	{System.Security.Cryptography.CryptographicException: Invalid input block size.   at Crimson.CommonCrypto.FastCryptorTransform.FinalDecrypt (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) [0x0000e] in /Developer/MonoTouch/Source/monotouch/src/CommonCrypto/FastCryptorTransform.cs:277    at Crimson.CommonCrypto.FastCryptorTransform.TransformFinalBlock (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) [0x0001e] in /Developer/MonoTouch/Source/monotouch/src/CommonCrypto/FastCryptorTransform.cs:345    at System.Security.Cryptography.CryptoStream.Read (System.Byte[] buffer, Int32 offset, Int32 count) [0x00240] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Security.Cryptography/CryptoStream.cs:179    at System.IO.StreamReader.ReadBuffer () [0x0000e] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.IO/StreamReader.cs:392    at System.IO.StreamReader.ReadLine () [0x00006] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.IO/StreamReader.cs:525    at MRIGiOSAESEncryptionLib.AESEncryption.DecryptStr (System.Byte[] encryptedString) [0x00039] in /Users/jmadden/src/MerMaid/Mermaid-iOS/iOSAESEncryptionLib/AESEnctyption.cs:90 }	System.Security.Cryptography.CryptographicException
Comment 2 Sebastien Pouliot 2013-08-26 14:13:35 UTC
I need a more complete test case, with actual values for `m_key`, `m_iv` and `encryptedString`.
Comment 3 Radu 2015-06-09 03:03:09 UTC
according to this 

this is more likely related to the ,manner in which you are creating the stream and the cryptostream
Comment 4 Radu 2015-06-09 03:31:47 UTC
this can also happen if you are trying to decrypt a string which is actually not an encrypted string
Comment 5 Sebastien Pouliot 2016-05-24 19:35:22 UTC
We have not received the requested information. If you are still experiencing this issue please provide all the requested information and re-open the bug report. Thanks!
Comment 6 Alberto 2017-11-20 12:06:34 UTC
I'm having the same problem. I provide mi example code:
public static string DecryptStringAES (string cipherText)
	string result = "";
	byte[] buffer = System.Text.Encoding.UTF8.GetBytes (cipherText);

	using (AesManaged aesManaged = new AesManaged ())
		aesManaged.KeySize = 128;
		aesManaged.Key = System.Text.Encoding.UTF8.GetBytes("1234567890123456");
		aesManaged.IV = System.Text.Encoding.UTF8.GetBytes("1234567890123456");
		aesManaged.Mode = CipherMode.CBC;
		ICryptoTransform transform = aesManaged.CreateDecryptor (aesManaged.Key, aesManaged.IV);
		using (MemoryStream memoryStream = new MemoryStream (buffer))
			using (CryptoStream cryptoStream = new CryptoStream (memoryStream, transform, CryptoStreamMode.Read))
				using (StreamReader streamReader = new StreamReader (cryptoStream))
					result = streamReader.ReadToEnd ();
	return result;

The string passed to the function is: