I’m writing to report about cryptography issue. The problem happens using RijndaelManaged class from the System.Security.Cryptography. It is important for me to use RijndaelManaged with CFB-8 (FeedbackSize = 8) mode without padding (PaddingMode.None). Such settings configuration makes encrypted data size equal to the decrypted data size.
Unfortunately Mono (Mono Compiler for MVS2010 IDE v2.0.8152) compiled code throws exception on data encryption with message:
[Unhandled Exception: System.Security.Cryptography.CryptographicException: invalid block length at Mono.Security.Cryptography.SymmetricTransform.FinalEncrypt].
I made tests with the .NET framework 4.0 under Windows XP and Windows 7, using the native Visual Studio 2010 compiler. I found that the native Microsoft .NET compiler does not throw any exceptions, and the code example works well.
Below I have pasted two examples (Repro code), one for Mono which throws an exception and one for native C# compiler in this case there are no exceptions. Also I have also pasted links to online compilers to test the code.
Why is the Mono Compiler throwing this exception?
Mono code Sample (Online compiler for testing, Compile Online)
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace Dela.Mono.Examples
{
public class HelloWorld
{
public static void Main(string[] args)
{
string plainText = "This will be encrypted.";
string plainText2 = "";
RijndaelManaged aesAlg = new RijndaelManaged();
aesAlg.BlockSize = 128;
aesAlg.KeySize = 256;
aesAlg.Mode = CipherMode.CFB;
aesAlg.FeedbackSize = 8;
aesAlg.Padding = PaddingMode.None;
aesAlg.GenerateKey();
aesAlg.GenerateIV();
ICryptoTransform encryptor = aesAlg.CreateEncryptor();
MemoryStream msEncrypt = new MemoryStream();
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) {
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) {
swEncrypt.Write(plainText);
}
}
Console.WriteLine(msEncrypt.ToArray().Length);
Console.WriteLine(System.Text.Encoding.UTF8.GetString(msEncrypt.ToArray()));
byte[] customArray = msEncrypt.ToArray();
ICryptoTransform decryptor = aesAlg.CreateDecryptor();
MemoryStream msDecrypt = new MemoryStream(customArray);
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
using (StreamReader swDecrypt = new StreamReader(csDecrypt)) {
plainText2 = swDecrypt.ReadToEnd();
}
}
Console.WriteLine(plainText2.Length);
Console.WriteLine(plainText2);
}
}
}
Native C# Code Sample (Online compiler for testing, Compile Online)
// Rextester.Program.Main is the entry point for your code. Don't change it.
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
string plainText = "This will be encrypted.";
string plainText2 = "";
RijndaelManaged aesAlg = new RijndaelManaged();
aesAlg.BlockSize = 128;
aesAlg.KeySize = 256;
aesAlg.Mode = CipherMode.CFB;
aesAlg.FeedbackSize = 8;
aesAlg.Padding = PaddingMode.None;
aesAlg.GenerateKey();
aesAlg.GenerateIV();
ICryptoTransform encryptor = aesAlg.CreateEncryptor();
MemoryStream msEncrypt = new MemoryStream();
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) {
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) {
swEncrypt.Write(plainText);
}
}
Console.WriteLine(msEncrypt.ToArray().Length);
Console.WriteLine(System.Text.Encoding.UTF8.GetString(msEncrypt.ToArray()));
byte[] customArray = msEncrypt.ToArray();
ICryptoTransform decryptor = aesAlg.CreateDecryptor();
MemoryStream msDecrypt = new MemoryStream(customArray);
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
using (StreamReader swDecrypt = new StreamReader(csDecrypt)) {
plainText2 = swDecrypt.ReadToEnd();
}
}
Console.WriteLine(plainText2.Length);
Console.WriteLine(plainText2);
}
}
}
The bug was fixed. The sources can be taken from the mono GIT repository.
master: e094d3dc0cf186f1de32d5340d847dc18aeca0e2
mono-2-10: 98e4842eb19dfd60000ada19e9bfb265fad7c84b