Hello,
Did anyone used DPAPI on Windows ?
I did tried to use declares and to try to get it work but so far it fails with error code 0 and nothing happens.
I guess Here you can find the details
Here is the code i tried
Function ProtectData(data As String) As MemoryBlock
#If TargetWindows
Declare Function CryptProtectData Lib "Crypt32.dll" (ByRef pDataIn As DATA_BLOB, szDataDescr As WString, pOptionalEntropy As Ptr, pvReserved As Ptr, pPromptStruct As Ptr, dwFlags As UInt32, ByRef pDataOut As DATA_BLOB) As Boolean
Declare Sub MemoryCopy Lib "kernel32" Alias "RtlMoveMemory" (dest As Ptr, src As Ptr, length As UInt32)
Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Ptr)
Declare Function GetLastError Lib "kernel32" () As UInt32
// Convert the string to a MemoryBlock
Var dataMB As MemoryBlock = data
// Prepare the input DATA_BLOB
Var inBlob As DATA_BLOB
inBlob.cbData = dataMB.Size
inBlob.pbData = dataMB
// Prepare the output DATA_BLOB
Var outBlob As DATA_BLOB
// Call CryptProtectData
Var result As Boolean = CryptProtectData(inBlob, "", Nil, Nil, Nil, 0, outBlob)
If result = False Then
Var errorCode As UInt32 = GetLastError()
// Use errorCode to determine the cause of the failure
MessageBox("CryptProtectData failed with error code: " + errorCode.ToString)
Return Nil
Else
// Ensure you copy the encrypted data from outBlob.pbData to encryptedData
Var encryptedData As New MemoryBlock(outBlob.cbData)
MemoryCopy(encryptedData, outBlob.pbData, outBlob.cbData)
// Now you can free the allocated memory
CoTaskMemFree(outBlob.pbData)
Return encryptedData
End If
#EndIf
End Function
As structures i have
Struct DATA_BLOB
cbData As UInt32
pbData As Ptr
End Struct
I guess here could be a complete example but in C# that shows both functions .
Thanks
3 posts - 3 participants