I have the following code:
Public Shared Function Crypt(text As String) As String
If text <> "" Then
Dim cryptoProvider As New DESCryptoServiceProvider()
Dim ms As New MemoryStream()
Dim cs As New CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_64, IV_64), CryptoStreamMode.Write)
Dim sw As New StreamWriter(cs)
sw.Write(text)
sw.Flush()
cs.FlushFinalBlock()
ms.Flush()
'convert back to a string
Return Convert.ToBase64String(ms.GetBuffer(), 0, CInt(ms.Length))
End If
Return ""
End Function
after a Fortify scan, they report that i need to release the cs CryptoStream object.
As far as i know, FlushFinalBlock() method do it this job.
Do I need call the disponse() function too? Or may be is a false-positive issue?
Any object that implements the
IDisposableinterface and is only used within the scope of a single block should be created with aUsingstatement. That way, it is guaranteed to be implicitly disposed at the end of theUsingblock. That applies even if aReturnstatement is hit or an exception is thrown. In your case, you are creating four disposable objects. There's no code required between the creation of each object or the destruction of each object so you don't need multiple nestedUsingblocks. You should use a singleUsingstatement for them all: