2

我写信是为了报告密码学问题。使用. RijndaelManaged_ System.Security.Cryptography对我来说,使用RijndaelManaged( CFB-8FeedbackSize = 8) 模式而不使用填充 ( PaddingMode.None) 很重要。此类设置配置使加密数据大小等于解密数据大小。

不幸的是,Mono (Mono Compiler for MVS2010 IDE v2.0.8152) 编译的代码在数据加密时抛出异常并显示消息:

[Unhandled Exception: System.Security.Cryptography.CryptographicException: invalid block length at Mono.Security.Cryptography.SymmetricTransform.FinalEncrypt]. 

我使用原生 Visual Studio 2010 编译器在 Windows XP 和 Windows 7 下使用 .NET framework 4.0 进行了测试。我发现本机 Microsoft .NET 编译器不会抛出任何异常,并且代码示例运行良好。

下面我粘贴了两个示例(复制代码),一个用于引发异常的 Mono,另一个用于本机 C# 编译器,在这种情况下没有异常。此外,我还粘贴了在线编译器的链接来测试代码。

为什么 Mono 编译器会抛出此异常?


Mono 代码示例(用于测试的在线编译器,在线编译

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);


      }
   } 
}

本机 C# 代码示例(用于测试的在线编译器,在线编译

// 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);
        }
    }
}

4

1 回答 1

2

该错误已修复。可以从单 GIT 存储库中获取源代码。

主:e094d3dc0cf186f1de32d5340d847dc18aeca0e2

单声道 2-10:98e4842eb19dfd60000ada19e9bfb265fad7c84b

于 2012-11-19T09:06:04.073 回答