我制作了一个聊天应用程序,并且使用AES算法。但是我看到此错误“输入数据不完整”。
这是我的代码:
public static string DecryptStringFromBytes_Aes(byte[] cipherText, string key, string mode)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
if (mode == "ECB") aesAlg.Mode = CipherMode.ECB;
else if (mode == "CBC") aesAlg.Mode = CipherMode.CBC;
else if (mode == "CFB") aesAlg.Mode = CipherMode.CFB;
aesAlg.Key = Convert.FromBase64String(key);
aesAlg.IV = ASCIIEncoding.ASCII.GetBytes(key.Substring(0, 16));
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
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.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
该错误在行中:plaintext = srDecrypt.ReadToEnd(); 我不知道为什么。
请帮帮我。非常感谢。