Hey luigi,
Would you happen to know much about VB6? I am looking to use the CryptUnprotectData function to decrypt these hex strings to their original form in VB6. Is the hex string just a simple way of storing the encrypted data (instead of storing a raw string with weird characters)?
I have checked google, but most of the code samples for the crypt32.dll or CryptUnprotectData were bloated, or I couldn't get them to work correctly with the hex string that BF2, .RDP, etc provide for us (maybe they want the string in a different form). I must have scanned through the first 5 pages of search results too :P, which is why I was hoping you knew some simple code that just gets the decryption done.
What would be nice is a to have a textbox with the hex string, another textbox to output the decrypted data, and a command button to initiate the function. Any ideas?
Edit: Here is some sample code, but clicking the button returns "0" to text2.text. I have also tried without the HextoAsc function, same result. Any ideas on tweaking it to work so it decrypts the string to text2.text?
Code:
Option Explicit
Private Type DATA_BLOB
cbData As Long
pbData As Long
End Type
Private Declare Function CryptProtectData _
Lib "crypt32.dll" ( _
ByRef pDataIn As DATA_BLOB, _
ByVal szDataDescr As String, _
ByRef pOptionalEntropy As Any, _
ByRef pvReserved As Any, _
ByRef pPromptStruct As Any, _
ByVal dwFlags As Long, _
ByRef pDataOut As DATA_BLOB) As Long
Private Declare Function CryptUnprotectData _
Lib "crypt32.dll" ( _
ByRef pDataIn As DATA_BLOB, _
ByVal ppszDataDescr As String, _
ByRef pOptionalEntropy As Any, _
pvReserved As Any, _
ByRef pPromptStruct As Any, _
ByVal dwFlags As Long, _
ByRef pDataOut As DATA_BLOB) As Long
Private Sub Command1_Click()
Dim udtDataIn As DATA_BLOB
Dim udtDataOut As DATA_BLOB
Dim sString As String
Dim aData() As Byte
sString = HexToAsc(Text1.Text)
aData() = sString
udtDataIn.cbData = UBound(aData)
udtDataIn.pbData = VarPtr(aData(0))
Text2.Text = CryptUnprotectData(udtDataIn, ByVal 0, ByVal vbNullString, ByVal vbNullString, ByVal vbNullString, ByVal 1, udtDataOut)
End Sub
Private Function HexToAsc(sString As String) As String
Dim X As Long
For X = 1 To Len(sString) Step 2
HexToAsc = HexToAsc & Chr$(Val("&H" & Mid$(sString, X, 2)))
Next
End Function