![](/img/trans.png)
[英]System.Security.Cryptography.CryptographicException: 'The input data is not a complete block.'
[英]System.Security.Cryptography.CryptographicException The input data is not a complete block
我對我的一個機密數據使用加密解密機制。 我對所有這些數據使用相同的加密解密方法。 我的大部分數據都得到了完美的加密和解密。 但是我的數據很少被正確加密但沒有被解密。 我在解密時收到“輸入數據不是完整塊”異常。 由於我無法解密,我無法識別受到影響的原始數據。 下面是我的全部代碼。
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;
}
}
在加密方法中,您必須在關閉 CryptoStream 后讀取 MemoryStream。 后者中可能存在未寫入前者的緩沖數據。
我也遇到了這個。 您需要添加一個:
csDecrypt.FlushFinalBlock();
就在您的 ReadToEnd 語句之前。 那應該可以解決問題。
可能是你的編碼有問題。 每當我使用GetBytes
時使用以下內容為我解決了問題:
var bytes = Encoding.GetEncoding(1252).GetBytes("whatever_text")
在將這些參數傳遞給解密方法時,您是否交替使用“密鑰”和“密文”? 我犯了同樣的錯誤並面臨此錯誤消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.