troubleshooting Question

decrypt the data using DPAPI, data is a base64-encoded encrypted string - VB.Net

Avatar of chokka
chokkaFlag for United States of America asked on
Visual Basic Classic.NET Programming
3 Comments1 Solution747 ViewsLast Modified:

I need to decrypt my Password which is stored in the database based on the DPAPI - Base 64 Encoded Encrypt Method.

I have the code - My code is not working. I am not able to debug. This code is extracted from the successfully running project.

' decrypt the data using DPAPI, data is a base64-encoded encrypted string
        Public Shared Function Decrypt(ByVal data As String, ByVal store As Store) As String
            ' holds the result string
            Dim result As String = ""

            ' blobs used in the CryptUnprotectData call
            Dim inBlob As New Win32.DATA_BLOB
            Dim entropyBlob As New Win32.DATA_BLOB
            Dim outBlob As New Win32.DATA_BLOB

            Try
                ' setup flags passed to the CryptUnprotectData call
                Dim flags As Integer = Win32.CRYPTPROTECT_UI_FORBIDDEN Or _
                 CInt(IIf(store = store.Machine, Win32.CRYPTPROTECT_LOCAL_MACHINE, 0))

                ' the CryptUnprotectData works with a byte array, convert string data
                Dim bits As Byte() = Convert.FromBase64String(data)

                ' setup input blobs, the data to be decrypted and entropy blob
                SetBlobData(inBlob, bits)
                SetBlobData(entropyBlob, Consts.EntropyData)

                ' call the DPAPI function, returns true if successful and fills in the outBlob
                If Win32.CryptUnprotectData(inBlob, Nothing, entropyBlob, IntPtr.Zero, IntPtr.Zero, flags, outBlob) Then
                    Dim resultBits As Byte() = GetBlobData(outBlob)
                    If Not resultBits Is Nothing Then
                        result = ASCIIEncoding.ASCII.GetString(resultBits)
                    End If
                End If
            Catch ex As Exception
                ' an error occurred, return an empty string
            Finally
                ' clean up
                If inBlob.pbData.ToInt32() <> 0 Then
                    Marshal.FreeHGlobal(inBlob.pbData)
                End If

                If entropyBlob.pbData.ToInt32() <> 0 Then
                    Marshal.FreeHGlobal(entropyBlob.pbData)
                End If
            End Try

            Return result
        End Function

  


 ' helper method that fills in a  DATA_BLOB, copies 
        ' data from managed to unmanaged memory
        Private Shared Sub SetBlobData(ByRef blob As Win32.DATA_BLOB, ByVal bits As Byte())
            blob.cbData = bits.Length
            blob.pbData = Marshal.AllocHGlobal(bits.Length)
            Marshal.Copy(bits, 0, blob.pbData, bits.Length)
        End Sub




        ' helper method that gets data from a DATA_BLOB, 
        ' copies data from unmanaged memory to managed
        Private Shared Function GetBlobData(ByRef blob As Win32.DATA_BLOB) As Byte()
            ' return an empty string if the blob is empty
            If blob.pbData.ToInt32() = 0 Then Return Nothing

            ' copy information from the blob
            Dim data(blob.cbData - 1) As Byte
            Marshal.Copy(blob.pbData, data, 0, blob.cbData)
            Win32.LocalFree(blob.pbData)

            Return data
        End Function
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros