Unchecked returned value causing unexpected states and conditions

I have been searching the internet for over an hour and can only find client side discussions the my latest scan finding. What I am receiving is method that uses the Read() method and because the Read() ignores the value returned could cause the program to overlook unexpected states and conditions finding. If anyone can explain, in small detail, and possibility recommend a fix the would be great. The function is below:

Offending line of code in the method:

csEncrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

Calling method:

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

-------------Problems Reply------------

Try not ignoring the return value:

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

What would happen in your code if fewer bytes were returned than you expected?

Category:asp.net Views:0 Time:2018-11-13

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.099 (s). 11 q(s)