未选中的返回值会导致意外的状态和条件

时间:2015-06-10 17:34:43

标签: asp.net stream cryptography cryptostream

我一直在网上搜索超过一个小时,只能找到客户端讨论我最新的扫描结果。我收到的是使用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);
    }

1 个答案:

答案 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);
        }
    }
}

如果返回的字节数少于预期,会在代码中发生什么?