System.Security.Cryptography.CryptographicException 输入数据不是一个

本文关键字:一个 Cryptography Security CryptographicException 输入 数据 System | 更新日期: 2023-09-27 18:30:34

我对我的一个机密数据使用加密解密机制。我对所有这些数据使用相同的加密解密方法。我的大部分数据都被完美地加密和解密。但是我的一些数据被正确加密但没有解密。我在解密时收到"输入数据不是完整的块"异常。由于我无法解密,因此我无法识别受影响的原始数据。下面是我的整个代码。

byte[] key = EncryptionHelper.ConvertStringToByteArray("2, 24, 2, 4, 26, 6, 20, 8, 16, 10, 12, 12, 10, 15, 18, 9, 17, 8, 19, 5, 21, 3, 25, 5");
byte[] intializationVector = EncryptionHelper.ConvertStringToByteArray("20, 221, 10, 140, 12, 185, 8, 19, 150, 212, 144, 26, 35, 88, 97, 82");
public static byte[] ConvertStringToByteArray(string inputString)
    {
        string[] sArray = null;
        List<byte> bList = new List<byte>();
        byte[] value = null;
        int i = 0;
        try
        {
            sArray = inputString.Split(new char[]{','});
            for (i = 0; i <= sArray.Length - 1; i++)
            {
                bList.Add((byte)Convert.ToInt32(sArray[i]));
            }
            value = bList.ToArray();
            return value;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

加密代码

public static byte[] Encrypt(string plainText, byte[] key, byte[] intializationVector)
    {
        byte[] result ;
        // Create a new instance of AES service provider
        using (Aes aesProvider = Aes.Create())
        {
            aesProvider.Key = key;
            aesProvider.IV = intializationVector;
            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = aesProvider.CreateEncryptor(aesProvider.Key, aesProvider.IV);

             // Create the streams used for encryption. 
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            //Write all data to the stream.
                            swEncrypt.Write(plainText);
                        }
                        result = msEncrypt.ToArray();
                    }
                }
        }
        return result;          
    }

解密代码

public static string Decrypt(byte[] inputInBytes, byte[] key, byte[] intializationVector)
{
        try
        {
            string result;
            // Create a new instance of AES service provider
            using (Aes aesProvider = Aes.Create())
            {
                aesProvider.Key = key;
                aesProvider.IV = intializationVector;
                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesProvider.CreateDecryptor(aesProvider.Key, aesProvider.IV);

                // Create the streams used for decryption. 
                using (MemoryStream msDecrypt = new MemoryStream(inputInBytes))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            result = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            return result;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

System.Security.Cryptography.CryptographicException 输入数据不是一个

在加密方法中,您必须在关闭CryptoStream后读取MemoryStream。后者中可能存在未写入前者的缓冲数据。

我也遇到了这个问题。 您需要添加:

 csDecrypt.FlushFinalBlock(); 

就在 ReadToEnd 语句之前。 这应该可以解决问题。

可能是您的编码有问题。每当我使用时使用以下方法GetBytes为我解决问题:

var bytes = Encoding.GetEncoding(1252).GetBytes("whatever_text")

在将这些参数传递给 decrypt 方法时,您是否交替使用"密钥"和"密文"?我犯了同样的错误并面对此错误消息。

相关文章: