System.Security.Cryptography.CryptographicException输入数据不是完整的块

时间:2014-11-17 07:00:44

标签: c# exception cryptography

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

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

 csDecrypt.FlushFinalBlock(); 

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

答案 2 :(得分:0)

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

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