CA2000:对象未沿所有异常路径处置

发布于 2024-12-01 04:35:21 字数 2681 浏览 3 评论 0原文

我无法弄清楚为什么在以下代码中收到此警告。

CA2000:Microsoft.Reliability:在方法“Encryption64.Decrypt(String, String)”中,对象“des”未沿着所有异常路径进行处置。在对对象“des”的所有引用超出范围之前,对对象“des”调用 System.IDisposable.Dispose。

CA2000:Microsoft.Reliability:在方法“Encryption64.Encrypt(String, String)”中,对象“des” ' 并未沿着所有异常路径进行处理。在对象“des”的所有引用超出范围之前调用 System.IDisposable.Dispose。

Public Class Encryption64

    Private key() As Byte = {}
    Private IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}

    Public Function Decrypt(ByVal stringToDecrypt As String, ByVal sEncryptionKey As String) As String

        Dim des As New DESCryptoServiceProvider()
        Dim ms As New MemoryStream()
        Dim ReturnValue As String = String.Empty

        Try

            Dim inputByteArray(stringToDecrypt.Length) As Byte
            key = System.Text.Encoding.UTF8.GetBytes(Left(sEncryptionKey, 8))
            inputByteArray = Convert.FromBase64String(stringToDecrypt)

            Dim cs As New CryptoStream(ms, des.CreateDecryptor(key, IV),CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()

            Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
            ReturnValue = encoding.GetString(ms.ToArray())

        Catch e As Exception

            ReturnValue = e.Message

        Finally

            If des IsNot Nothing Then
                des.Dispose()
            End If

            If ms IsNot Nothing Then
                ms.Dispose()
            End If

        End Try

        Return ReturnValue

    End Function

    Public Function Encrypt(ByVal stringToEncrypt As String, ByVal SEncryptionKey As String) As String

        Dim des As New DESCryptoServiceProvider()
        Dim ms As New MemoryStream()
        Dim ReturnValue As String = String.Empty

        Try

            key = System.Text.Encoding.UTF8.GetBytes(Left(SEncryptionKey, 8))

            Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(stringToEncrypt)
            Dim cs As New CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write)

            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()

            ReturnValue = Convert.ToBase64String(ms.ToArray())

        Catch e As Exception

            ReturnValue = e.Message

        Finally

            If des IsNot Nothing Then
                des.Dispose()
            End If

            If ms IsNot Nothing Then
                ms.Dispose()
            End If

        End Try

        Return ReturnValue

    End Function

End Class

I am having trouble trying to figure out why I'm getting this warning in following code.

CA2000 : Microsoft.Reliability : In method 'Encryption64.Decrypt(String, String)', object 'des' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'des' before all references to it are out of scope.

CA2000 : Microsoft.Reliability : In method 'Encryption64.Encrypt(String, String)', object 'des' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'des' before all references to it are out of scope.

Public Class Encryption64

    Private key() As Byte = {}
    Private IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}

    Public Function Decrypt(ByVal stringToDecrypt As String, ByVal sEncryptionKey As String) As String

        Dim des As New DESCryptoServiceProvider()
        Dim ms As New MemoryStream()
        Dim ReturnValue As String = String.Empty

        Try

            Dim inputByteArray(stringToDecrypt.Length) As Byte
            key = System.Text.Encoding.UTF8.GetBytes(Left(sEncryptionKey, 8))
            inputByteArray = Convert.FromBase64String(stringToDecrypt)

            Dim cs As New CryptoStream(ms, des.CreateDecryptor(key, IV),CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()

            Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
            ReturnValue = encoding.GetString(ms.ToArray())

        Catch e As Exception

            ReturnValue = e.Message

        Finally

            If des IsNot Nothing Then
                des.Dispose()
            End If

            If ms IsNot Nothing Then
                ms.Dispose()
            End If

        End Try

        Return ReturnValue

    End Function

    Public Function Encrypt(ByVal stringToEncrypt As String, ByVal SEncryptionKey As String) As String

        Dim des As New DESCryptoServiceProvider()
        Dim ms As New MemoryStream()
        Dim ReturnValue As String = String.Empty

        Try

            key = System.Text.Encoding.UTF8.GetBytes(Left(SEncryptionKey, 8))

            Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(stringToEncrypt)
            Dim cs As New CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write)

            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()

            ReturnValue = Convert.ToBase64String(ms.ToArray())

        Catch e As Exception

            ReturnValue = e.Message

        Finally

            If des IsNot Nothing Then
                des.Dispose()
            End If

            If ms IsNot Nothing Then
                ms.Dispose()
            End If

        End Try

        Return ReturnValue

    End Function

End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈独秀 2024-12-08 04:35:21

由于您在 Try ... Final 块之外声明(并实例化) des 对象,因此您的代码可能会在 Dim ms As New MemoryStream() 行中引发异常,并且您的 .Dispose() 将不会被叫。

当您使用实现 IDisposable 的对象时,最好尽可能将它们包装在 using 块中,而不是 Try...Finally 块中。例如:

公共函数 Decrypt(ByVal stringToDecrypt As String, ByVal sEncryptionKey As String) As String

    Dim ms As New MemoryStream() 
    Dim ReturnValue As String = String.Empty 

        Dim inputByteArray(stringToDecrypt.Length) As Byte 
        key = System.Text.Encoding.UTF8.GetBytes(Left(sEncryptionKey, 8)) 
        inputByteArray = Convert.FromBase64String(stringToDecrypt) 

        Using ms as New MemoryStream
           Using des As New DESCryptoServiceProvider
              Dim cs As New CryptoStream(ms, des.CreateDecryptor(key, IV),CryptoStreamMode.Write) 
              cs.Write(inputByteArray, 0, inputByteArray.Length) 
              cs.FlushFinalBlock() 
           End Using ' des
           Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8 
           ReturnValue = encoding.GetString(ms.ToArray()) 
        End Using ' ms 
    Catch e As Exception 

        ReturnValue = e.Message 

    End Try 

    Return ReturnValue 

End Function 

Since you are declaring (and instantiating) your des objects outside of the Try ... Finally blocks, it is possible for your code to raise an exception in the line Dim ms As New MemoryStream() and your .Dispose() will not be called.

When you are working with objects that implement IDisposable, it is much preferable where possible to wrap them in a Using block instead of a Try...Finally block. For example:

Public Function Decrypt(ByVal stringToDecrypt As String, ByVal sEncryptionKey As String) As String

    Dim ms As New MemoryStream() 
    Dim ReturnValue As String = String.Empty 

        Dim inputByteArray(stringToDecrypt.Length) As Byte 
        key = System.Text.Encoding.UTF8.GetBytes(Left(sEncryptionKey, 8)) 
        inputByteArray = Convert.FromBase64String(stringToDecrypt) 

        Using ms as New MemoryStream
           Using des As New DESCryptoServiceProvider
              Dim cs As New CryptoStream(ms, des.CreateDecryptor(key, IV),CryptoStreamMode.Write) 
              cs.Write(inputByteArray, 0, inputByteArray.Length) 
              cs.FlushFinalBlock() 
           End Using ' des
           Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8 
           ReturnValue = encoding.GetString(ms.ToArray()) 
        End Using ' ms 
    Catch e As Exception 

        ReturnValue = e.Message 

    End Try 

    Return ReturnValue 

End Function 
好倦 2024-12-08 04:35:21

只是一个猜测,但也许它不够聪明,无法意识到此代码行将始终为真:

If des IsNot Nothing Then

换句话说,它可能假设因为存在条件语句,所以 Dispose() 调用可能不会被处决。

要进行检查,您可以尝试注释掉“if”并查看警告是否消失。

Just a guess, but maybe it's not smart enough to realize that this code line will always be true:

If des IsNot Nothing Then

In other words, it might assume that because there is a conditional statement, the Dispose() call might not be executed.

To check, you can try commenting out the "if" and see if the warning goes away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文