[Mono-list] RijndaelManaged class issue using CFB-8 mode

vdm netmonitoring at mail.ru
Fri Sep 14 21:26:28 UTC 2012


Hi.

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 (in my situation it is important).
Unfortunately mono [in my pc it is mono compiler for MVS2010 IDE version
2.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]. Also i made
tests with the .NET framework 4.0 under Windows XP and Windows 7 using
native Visual Studio 2010 compiler and found that microsoft compiler does
not throw any exceptions and the code example works well using native .NET
compiler. What is the problem with mono compiler, why exception rise? Below
i paste two examples (repro code), one for mono which throws exceptions and
one for native C# compiler in this case there are no exceptions. Also i
paste online compilers links to test the codes.

Why code compiled by mono crashes and how to solve it?

##################################################################################################################################

Mono code sample [Link for online compiler to test:
http://www.compileonline.com/compile_csharp_online.php]

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 [Link for online compiler to test:
http://rextester.com/runcode]

//Title of this code
//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);
        }
    }
}

##################################################################################################################################
   



--
View this message in context: http://mono.1490590.n4.nabble.com/RijndaelManaged-class-issue-using-CFB-8-mode-tp4656640.html
Sent from the Mono - General mailing list archive at Nabble.com.


More information about the Mono-list mailing list