我一直在网上搜索超过一个小时,只能找到客户端讨论我最新的扫描结果。我收到的是使用Read()方法的方法,因为Read()忽略返回的值可能导致程序忽略意外状态和条件查找。如果有人能够详细解释,并且有可能推荐修复,那就太棒了。功能如下:
方法中的违规行代码:
csEncrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
通话方式:
public String DecryptMessage(byte[] encrypted)
{
ASCIIEncoding textConverter = new ASCIIEncoding();
decryptor = aes.CreateDecryptor(key, IV);
MemoryStream msDecrypt = new MemoryStream(encrypted);
csEncrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
byte[] fromEncrypt = new byte[encrypted.Length];
csEncrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
return textConverter.GetString(fromEncrypt);
}
答案 0 :(得分:0)
尽量不要忽略返回值:
public String DecryptMessage(byte[] encrypted)
{
ASCIIEncoding textConverter = new ASCIIEncoding();
decryptor = aes.CreateDecryptor(key, IV);
using (MemoryStream msDecrypt = new MemoryStream(encrypted))
{
using (var csEncrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
byte[] fromEncrypt = new byte[encrypted.Length];
var bytesRead = csEncrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
return textConverter.GetString(fromEncrypt, 0, bytesRead);
}
}
}
如果返回的字节数少于预期,会在代码中发生什么?