另一个“要解密的数据长度无效。”错误

发布于 2024-11-03 02:13:33 字数 4343 浏览 1 评论 0原文

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

我确信我错过了一些基本的东西,但我看不出它是什么。

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

我创建了一个控制台应用程序来说明问题。完整代码如下:

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

I am receiving a 'Length of the data to decrypt is invalid.' error when attempting to decrypt a string. I have looked at a number of other references to this error on this site, and tried a number of the suggestions found there, but so far nothing has worked.

I'm sure that I'm missing something fundamental, but I can't see what it is.

I'm using the same key and IV when encrypting and decrypting. I added FlushFinalBlock() calls when encrypting and decrypting. I had even attempted to set encryptStream.Position = 0, but that throws an ObjectDisposedException.

I created a console app that illustrates the problem. The code is below in its entirety:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2024-11-10 02:13:33

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

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

string encryptedString = Convert.ToBase64String(encryptedBytes);

解密方法中,第一行是:

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

You can't convert a binary array to UTF-8 - they're not the same thing. Use Base64 instead.

Inside the encrypt method, the second to last line should be:

string encryptedString = Convert.ToBase64String(encryptedBytes);

And the decrypt method, the first line is:

byte[] encryptedBytes = Convert.FromBase64String(encryptedString);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文