C# 4.0 另一个';要解密的数据长度无效;错误

C# 4.0 另一个';要解密的数据长度无效;错误,c#-4.0,encryption,rijndael,C# 4.0,Encryption,Rijndael,我收到一个“要解密的数据长度无效”。尝试解密字符串时出错。我在这个网站上查看了许多其他关于这个错误的参考资料,并尝试了一些在那个里找到的建议,但到目前为止并没有任何效果 我确信我遗漏了一些基本的东西,但我看不出是什么 我在加密和解密时使用相同的密钥和IV。我在加密和解密时添加了FlushFinalBlock()调用。我甚至尝试设置encryptStream.Position=0,但这会引发ObjectDisposedException 我创建了一个控制台应用程序来说明这个问题。该守则全文如下:

我收到一个“要解密的数据长度无效”。尝试解密字符串时出错。我在这个网站上查看了许多其他关于这个错误的参考资料,并尝试了一些在那个里找到的建议,但到目前为止并没有任何效果

我确信我遗漏了一些基本的东西,但我看不出是什么

我在加密和解密时使用相同的密钥和IV。我在加密和解密时添加了
FlushFinalBlock()
调用。我甚至尝试设置
encryptStream.Position=0
,但这会引发
ObjectDisposedException

我创建了一个控制台应用程序来说明这个问题。该守则全文如下:

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Crypto
{
    class Program
    {
        static void Main(string[] args)
        {
            _CryptoString = AESStringEncryption(CRYPTO_STRING);
            _CryptoString = AESStringDecryption(_CryptoString);
        }

        private const string CRYPTO_STRING = "The quick brown fox jumped over the lazy dog.";

        private static byte[] _KY = { 47, 53, 94, 65, 243, 197, 42, 80, 125, 255, 144, 41, 130, 76, 2, 142, 43, 1, 120, 124, 255, 248, 232, 139, 170, 42, 243, 52, 4, 17, 60, 174 };
        private static byte[] _VI = { 68, 42, 157, 47, 99, 8, 174, 169, 119, 255, 144, 218, 8, 30, 60, 42 };

        private static string _CryptoString;

        /// <summary>
        /// Applies .NET Framework AES (Advanced Encryption Standard) Level encryption to the provided string parameter.
        /// Utilizies the UTF8 encoding stardard for the conversion from string to byte[].
        /// </summary>
        public static string AESStringEncryption(string unencryptedString)
        {
            byte[] unencryptedBytes = Encoding.UTF8.GetBytes(unencryptedString);
            byte[] encryptedBytes = AESByteArrayEncryption(unencryptedBytes);
            string encryptedString = Encoding.UTF8.GetString(encryptedBytes);

            return encryptedString;
        }

        /// <summary>
        /// Applies .NET Framework AES (Advanced Encryption Standard) Level decryption to the provided string parameter.
        /// Utilizies the UTF8 encoding stardard for the conversion from byte[] to string.
        /// </summary>
        public static string AESStringDecryption(string encryptedString)
        {
            byte[] encryptedBytes = Encoding.UTF8.GetBytes(encryptedString);
            byte[] decryptedBytes = AESByteArrayDecryption(encryptedBytes);
            string decryptedString = Encoding.UTF8.GetString(decryptedBytes);

            return decryptedString;
        }

        /// <summary>
        /// Applies .NET Framework AES (Advanced Encryption Standard) Level encryption to the provided byte array parameter.
        /// </summary>
        public static byte[] AESByteArrayEncryption(byte[] unencryptedBytes)
        {
            using (var rm = new RijndaelManaged())
            {
                var encryptor = rm.CreateEncryptor(_KY, _VI);
                using (var encryptStream = new MemoryStream())
                {
                    using (var cryptoStream = new CryptoStream(encryptStream, encryptor, CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(unencryptedBytes, 0, unencryptedBytes.Length);
                        cryptoStream.FlushFinalBlock();
                    }

                    //encryptStream.Position = 0;

                    byte[] encryptedBytes = encryptStream.ToArray();

                    return encryptedBytes;
                }
            }
        }

        /// <summary>
        /// Applies .NET Framework AES (Advanced Encryption Standard) Level decryption to the provided byte array parameter.
        /// </summary>
        public static byte[] AESByteArrayDecryption(byte[] encryptedBytes)
        {
            using (var rm = new RijndaelManaged())
            {
                var decryptor = rm.CreateDecryptor(_KY, _VI);
                using (var decryptStream = new MemoryStream())
                {
                    using (var cryptoStream = new CryptoStream(decryptStream, decryptor, CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(encryptedBytes, 0, encryptedBytes.Length);
                        cryptoStream.FlushFinalBlock();
                    }

                    //decryptStream.Position = 0;

                    byte[] decryptedBytes = decryptStream.ToArray();

                    return decryptedBytes;
                }
            }
        }
    }
}
使用系统;
使用System.IO;
使用System.Security.Cryptography;
使用系统文本;
命名空间加密
{
班级计划
{
静态void Main(字符串[]参数)
{
_CryptoString=AESStringEncryption(加密字符串);
_CryptoString=AESStringDecryption(_CryptoString);
}
private const string CRYPTO_string=“敏捷的棕色狐狸跳过了懒狗。”;
私有静态字节[]_KY={47、53、94、65、243、197、42、80、125、255、144、41、130、76、2、142、43、1、120、124、255、248、232、139、170、42、243、52、4、17、60、174};
私有静态字节[]_VI={68,42,157,47,99,8,174,169,119,255,144,218,8,30,60,42};
私有静态字符串_CryptoString;
/// 
///将.NET Framework AES(高级加密标准)级别的加密应用于提供的字符串参数。
///利用UTF8编码标准将字符串转换为字节[]。
/// 
公共静态字符串AESStringEncryption(字符串未加密字符串)
{
byte[]unencryptedBytes=Encoding.UTF8.GetBytes(unencryptedString);
byte[]encryptedBytes=AESByteArrayEncryption(未加密字节);
string encryptedString=Encoding.UTF8.GetString(encryptedBytes);
返回encryptedString;
}
/// 
///将.NET Framework AES(高级加密标准)级别的解密应用于提供的字符串参数。
///利用UTF8编码标准将字节[]转换为字符串。
/// 
公共静态字符串解密(字符串加密字符串)
{
byte[]encryptedBytes=Encoding.UTF8.GetBytes(encryptedString);
byte[]decryptedBytes=AESByteArrayDecryption(encryptedBytes);
string decryptedString=Encoding.UTF8.GetString(decryptedBytes);
返回解密字符串;
}
/// 
///将.NET Framework AES(高级加密标准)级别的加密应用于提供的字节数组参数。
/// 
公共静态字节[]AESByteArrayEncryption(字节[]未加密字节)
{
使用(var rm=new RijndaelManaged())
{
var encryptor=rm.CreateEncryptor(_-KY,_-VI);
使用(var encryptStream=new MemoryStream())
{
使用(var cryptoStream=新加密流(encryptStream、encryptor、CryptoStreamMode.Write))
{
cryptoStream.Write(未加密字节,0,未加密字节.长度);
cryptoStream.FlushFinalBlock();
}
//加密流。位置=0;
byte[]encryptedBytes=encryptStream.ToArray();
返回encryptedBytes;
}
}
}
/// 
///将.NET Framework AES(高级加密标准)级别的解密应用于提供的字节数组参数。
/// 
公共静态字节[]AESByteArrayDecryption(字节[]encryptedBytes)
{
使用(var rm=new RijndaelManaged())
{
var decryptor=rm.CreateDecryptor(_-KY,_-VI);
使用(var decryptStream=newmemoryStream())
{
使用(var cryptoStream=new cryptoStream(decryptostream,decryptoctor,CryptoStreamMode.Write))
{
cryptoStream.Write(encryptedBytes,0,encryptedBytes.Length);
cryptoStream.FlushFinalBlock();
}
//位置=0;
byte[]decryptedBytes=decryptStream.ToArray();
返回解密字节;
}
}
}
}
}

您不能将二进制数组转换为UTF-8-它们不是一回事。请改用Base64

在encrypt方法中,从第二行到最后一行应该是:

string encryptedString = Convert.ToBase64String(encryptedBytes);
而解密方法,第一行是:

byte[] encryptedBytes = Convert.FromBase64String(encryptedString);

工作得很好!谢谢我知道这一定很简单,效果很好。。谢谢@Weltonv3.56