c#-4.0 - 另一个 'Length of the data to decrypt is invalid.' 错误

标签 c#-4.0 encryption rijndael

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

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

我在加密和解密时使用相同的 key 和 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;
                }
            }
        }
    }
}

最佳答案

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

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

string encryptedString = Convert.ToBase64String(encryptedBytes);

还有解密方法,第一行是:

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

关于c#-4.0 - 另一个 'Length of the data to decrypt is invalid.' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5758739/

相关文章:

javascript - 如何解密 node.js 中使用 Rijndael-256 加密的消息?

c# - 部分读取最终 block 后的填充错误

c#-4.0 - 在 Dispatcher.Invoke() 中使用 lambda 表达式作为参数

c# - 将 CheckedListBox 绑定(bind)到设置

c# - 加密配置文件段(Settings.settings)

java - keystore 操作因 RSA 签名和验证失败

Java 文件 I/O 不适用于外部库方法

c# - 根据枚举值获取枚举名称

asp.net-mvc-3 - 无法转换 WhereSelectListIterator 类型的对象

.net - .NET AES/Rijndael-重用解密器时解密不一致