Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15634

VS 2017 Disposing variables and objects that contains sensitive data

$
0
0
I made a class to retrieve sensitive encrypted data from a file.

The class ClassSecret have a ReadOnly Property that return the sensitive data into a Dictionary.
The class DataProtector contains the function to decrypt the encrypted data.

Code:

Imports System.Web.Script.Serialization

Public Class ClassSecret
    Private Shared ReadOnly byteArray As Byte() = {0, 0} 'The sensitive data

    Private Shared ReadOnly Property Secrets As Dictionary(Of String, String)
        Get
            Dim dp As DataProtector = Nothing
            Dim tmpSecretsString As String
            Dim tmpSecrets As Dictionary(Of String, String)
            Dim serializer As JavaScriptSerializer = Nothing

            Try
                dp = New DataProtector
                tmpSecretsString = dp.ProtectedDataToString(byteArray)
                serializer = New JavaScriptSerializer()
                tmpSecrets = serializer.Deserialize(Of Dictionary(Of String, String))(tmpSecretsString)
                Return tmpSecrets
            Finally
                dp = Nothing
                tmpSecretsString = Nothing
                tmpSecrets = Nothing
                serializer = Nothing
            End Try

        End Get
    End Property

End Class

Public Class DataProtector
    Public Function ProtectedDataToString(ByVal data As Byte()) As String
        'Do some stuff
        Dim newString As String = "{""decrypted"":""data""}"
        Return newString
    End Function
End Class

I don't have much knowledge in computer science. I'm not sure if the variables values are stored somewhere in memory (and then can be accessed by unauthorized app) after I get the "Secrets" Property value from somewhere else in my program.
That's why I used a Try Finally to make the variables and objects egual to Nothing after the value of the property is returned.
I'm not using the Using statement since my class DataProtector don't have IDisposable implemented.

I may overcomplicate everything here. Is that Finally statement to make variables egual to Nothing is usefull, or all the variables are somewhat disposed automatically after the Get method returned the value as in the following code?

Code:

Private Shared ReadOnly Property Secrets As Dictionary(Of String, String)
        Get
            Dim tmpSecretsString As String = New DataProtector().ProtectedDataToString(byteArray)
            Dim tmpSecrets As Dictionary(Of String, String) = New JavaScriptSerializer().Deserialize(Of Dictionary(Of String, String))(tmpSecretsString)

            Return tmpSecrets
        End Get
    End Property

Thanks

Viewing all articles
Browse latest Browse all 15634

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>