[Mono-bugs] [Bug 781683] New: Mono.Security.Cryptography RijndaelManaged class issue using CFB-8 mode

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Sat Sep 22 20:48:31 UTC 2012


https://bugzilla.novell.com/show_bug.cgi?id=781683

https://bugzilla.novell.com/show_bug.cgi?id=781683#c0


           Summary: Mono.Security.Cryptography RijndaelManaged class issue
                    using CFB-8 mode
    Classification: Mono
           Product: MonoTouch
           Version: unspecified
          Platform: All
        OS/Version: Windows 7
            Status: NEW
          Severity: Critical
          Priority: P5 - None
         Component: Class Libraries
        AssignedTo: frego at suse.com
        ReportedBy: netmonitoring at mail.ru
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---
           Blocker: ---


User-Agent:       Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101
Firefox/15.0.1

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.

Also i have found that if data which will be encrypted has length which is
devided by 16 then exception does not rise. But in my case i have set
FeedBackSize to 8 (1 byte), so with such setting there should not be matter if
data length is devided by 16 or not. Impression that mono cryptagraphy library
just does not use FeedBackSize parameter correctly with RijndaelManaged class
on CFB mode?

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

Reproducible: Always

Steps to Reproduce:
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);
        }
    }
}

-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.


More information about the mono-bugs mailing list