Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6354671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:36:52+00:00 2026-05-24T22:36:52+00:00

I am having trouble trying to figure out why I’m getting this warning in

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:36:52+00:00Added an answer on May 24, 2026 at 10:36 pm

    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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a bit of a trouble trying to figure this out today, i
I'm having trouble trying to figure out how to achieve this programming challenge in
I been having trouble trying to figure this out. When I think I have
Im having trouble trying to figure out some code someone has suggested and was
having some trouble trying to figure this out, complete novice when it come to
I am having some trouble trying to figure this out. What i have is
This query works fine but I'm having trouble trying to figure out why it
I'm having trouble trying to figure out why my formatDate is not working correctly.
I'm having trouble trying to figure out how to get prolog to spit out
Hey im new to database design and having trouble trying to figure this one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.