C# · 3月 29, 2022

c#-4.0 – 另一个“要解密的数据长度无效.”错误

我收到的’要解密的数据长度无效.’尝试解密字符串时出错.我在这个网站上看了很多其他对这个错误的引用,并尝试了一些在那里找到的建议,但到目前为止没有任何工作.

我确信我缺少一些基本的东西,但我看不出它是什么.

我在加密和解密时使用相同的密钥和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,248,232,139,170,52,4,17,60,174 }; private static byte[] _VI = { 68,157,47,99,8,174,169,119,218,30,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,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,encryptedBytes.Length); cryptoStream.FlushFinalBlock(); } //decryptStream.Position = 0; byte[] decryptedBytes = decryptStream.ToArray(); return decryptedBytes; } } } }}解决方法 你不能将二进制数组转换为UTF-8 – 它们不是同一个东西.请改用Base64.

在加密方法中,倒数第二行应该是:

string encryptedString = Convert.ToBase64String(encryptedBytes);

而解密方法,第一行是:

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