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

Posted on 2009-12-24
Last Modified: 2013-11-25

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


                ' 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


                ' clean up

                If inBlob.pbData.ToInt32() <> 0 Then


                End If

                If entropyBlob.pbData.ToInt32() <> 0 Then


                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)


            Return data

        End Function

Open in new window

Question by:chokka
    LVL 10

    Accepted Solution


    Author Comment

    I verified couple of links like this ..!

    Anyways, i will recheck it ..!

    My issue is that, I can Encrypt with my code..! But i am not able to Decrypt ..!

    Let me check with the links provided and update you shortly ..!


    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now