'System.Security.Cryptography.CryptographicException The input data is not a complete block
I use Encryption Decryption mechanism for one of my confidential data. I use the same Encryption Decryption methods for all of those data. Most of my data are getting Encrpyted and Decrypted back perfectly. But few of my data are Encrypted correctly but are not getting Decrypted. I receive the "The input data is not a complete block" exception while Decrypting. Since I am not able to Decrypt I am not able to identify the original data which are getting affected. Below is my entire code.
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;
}
}
Encryption Code
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;
}
Decryption Code
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;
}
}
Solution 1:[1]
In the encryption method you have to read your MemoryStream AFTER you close your CryptoStream. There may be buffered data in the latter which isn't written to the former.
Solution 2:[2]
I ran into this too. You need to add a:
csDecrypt.FlushFinalBlock();
right before your ReadToEnd statement. That should solve the problem.
Solution 3:[3]
Could be an issue with your encoding. Using the following whenever I used GetBytes fixed the problem for me:
var bytes = Encoding.GetEncoding(1252).GetBytes("whatever_text")
Solution 4:[4]
Did you use the "key" and "ciphertext" interchangeably when passing these parameters to the decrypt method? I did the same mistake and faced this error message.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fejesjoco |
| Solution 2 | The Professor |
| Solution 3 | |
| Solution 4 | Rajiv Ganti |
