Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 533
  • Last Modified:

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

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

  • 2
1 Solution
chokkaAuthor Commented:
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 ..!

chokkaAuthor Commented:

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now