# DPAPI On Windows

**URL:** <https://forum.xojo.com/t/dpapi-on-windows/79521>\
**Category:** Windows\
**Created:** [March 8, 2024, 9:07pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521 "2024-03-08T21:07:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aurelian\_N](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/b4bc9f/32.png) [@Aurelian\_N](https://forum.xojo.com/u/Aurelian_N)\
**Post date:** [March 8, 2024, 9:07pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/1 "2024-03-08T21:07:25Z")

</div>

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](https://learn.microsoft.com/en-us/windows/win32/api/dpapi/nf-dpapi-cryptprotectdata)

Here is the code i tried

```auto
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 .](https://learn.microsoft.com/en-us/windows/win32/seccrypto/example-c-program-using-cryptprotectdata)

Thanks

---

<div class="post-metadata">

**Author:** ![Jerry\_Fritschle](https://forum.xojo.com/user_avatar/forum.xojo.com/jerry_fritschle/32/223_2.png) [@Jerry\_Fritschle](https://forum.xojo.com/u/Jerry_Fritschle)\
**Post date:** [March 9, 2024, 12:08am UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/2 "2024-03-09T00:08:12Z")

</div>

> [@Aurelian\_N](#):
>
> but so far it fails with error code 0 and nothing happens.

This seems strange to me. With functions that I’m familiar with, that return error codes, zero means success. Sorry, that’s all I can offer.

---

<div class="post-metadata">

**Author:** ![Andrew\_Lambert](https://forum.xojo.com/user_avatar/forum.xojo.com/andrew_lambert/32/215_2.png) [@Andrew\_Lambert](https://forum.xojo.com/u/Andrew_Lambert)\
**Post date:** [March 9, 2024, 1:52am UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/3 "2024-03-09T01:52:50Z")

</div>

The code works for me on 32 bit and fails on 64 bit. I can get 64 bit to work if I set the StructureAlignment attribute of the `DATA_BLOB` structure to 8

 ![Untitled](https://forum.xojo.com/uploads/default/original/3X/a/b/ab3099b6f9880787533bca705cbc50f1e28e2c07.png)

Note how the members of the structure are no longer contiguous.

---

<div class="post-metadata">

**Author:** ![Aurelian\_N](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/b4bc9f/32.png) [@Aurelian\_N](https://forum.xojo.com/u/Aurelian_N)\
**Post date:** [March 9, 2024, 9:10am UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/4 "2024-03-09T09:10:05Z")

</div>

> [@Aurelian\_N](#):
>
> ```auto
> #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
> 
> ```

The usual 64 bit issue, Thanks a lot Andrew

---

<div class="post-metadata">

**Author:** ![Eugene\_Dakin](https://forum.xojo.com/user_avatar/forum.xojo.com/eugene_dakin/32/178_2.png) [@Eugene\_Dakin](https://forum.xojo.com/u/Eugene_Dakin)\
**Post date:** [March 9, 2024, 12:11pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/5 "2024-03-09T12:11:27Z")

</div>

Hello Gentlemen,

Andrew answered the question and this is a version converted from C++ to Xojo.

Here is an example which works in 32-bit and 64-bit mode:

```auto
Sub Pressed() Handles Pressed
  //Works in 32-bit and 64-bit phase
  TextArea1.Text = ""
  
  Var pbDataInput as string = "Hello world of data protection."
  Var mbDataInput as New MemoryBlock(pbDataInput.Length+1)
  mbDataInput.CString(0) = pbDataInput
  
  #If Target32Bit
    Var DataIn as DATA_BLOB_32
    Var DataOut as DATA_BLOB_32
    Var DataVerify as DATA_BLOB_32
  #Else //Target 64 bit
    Var DataIn as DATA_BLOB_64
    Var DataOut as DATA_BLOB_64
    Var DataVerify as DATA_BLOB_64
  #EndIf
  
  TextArea1.Text = TextArea1.Text + "DataIn: " + DataIn.Size.ToString + " bytes" + EndOfLine
  Var dbDataInput as UInt32 
  dbDataInput = pbDataInput.Length + 1
  TextArea1.Text = TextArea1.Text + "pbDataInput: " + dbDataInput.ToString + EndOfLine
  TextArea1.Text = TextArea1.Text + "mbDataInput: " + mbDataInput.Size.ToString + EndOfLine
  DataIn.pbData = mbDataInput
  DataIn.cbData = mbDataInput.Size
  
  Var PromptStruct as CRYPTPROTECT_PROMPTSTRUCT
  PromptStruct.cbSize = PromptStruct.Size
  PromptStruct.dwPromptFlags = &h02
  PromptStruct.szPrompt = "This is a user prompt."
  
  //Begin protect phase
  #If Target32Bit
    Declare Function CryptProtectData Lib "Crypt32.dll" (ByRef pDataIn As Data_Blob_32, szDataDescr As WString, pOptionalEntropy As Ptr, pvReserved As Ptr, ByRef pPromptStruct As CRYPTPROTECT_PROMPTSTRUCT, dwFlags As UInt32, ByRef pDataOut As Data_Blob_32) As Boolean
  #Else //64 bit
    Declare Function CryptProtectData Lib "Crypt32.dll" (ByRef pDataIn As Data_Blob_64, szDataDescr As WString, pOptionalEntropy As Ptr, pvReserved As Ptr, ByRef pPromptStruct As CRYPTPROTECT_PROMPTSTRUCT, dwFlags As UInt32, ByRef pDataOut As Data_Blob_64) As Boolean
  #EndIf
  
  If (CryptProtectData(DataIn, "This is the description string.", Nil, Nil, PromptStruct, 0, DataOut) = True) Then
    TextArea1.Text = TextArea1.Text + "The encryption phase worked." + EndOfLine
    TextArea1.Text = TextArea1.Text + "DataOut " + DataOut.cbData.ToString + EndOfLine
  Else
    TextArea1.Text = TextArea1.Text + "Encryption Error"
  End If
  
  //Begin the unprotect phase
  TextArea1.Text = TextArea1.Text + "Begin the unprotect phase" + EndOfLine
  #If Target32Bit
    Declare Function CryptUnprotectData Lib "Crypt32.dll" (ByRef pDataIn As Data_Blob_32, ppszDataDescr As WString, pOptionalEntropy As Ptr, pvReserved As Ptr, ByRef pPromptStruct As CRYPTPROTECT_PROMPTSTRUCT, dwFlags As UInt32, ByRef pDataOut As Data_Blob_32) As Boolean
  #Else //Target 64 bit
    Declare Function CryptUnprotectData Lib "Crypt32.dll" (ByRef pDataIn As Data_Blob_64, ppszDataDescr As WString, pOptionalEntropy As Ptr, pvReserved As Ptr, ByRef pPromptStruct As CRYPTPROTECT_PROMPTSTRUCT, dwFlags As UInt32, ByRef pDataOut As Data_Blob_64) As Boolean
  #EndIf
  
  Var pDescrOut as WString
  If (CryptUnprotectData(DataOut, pDescrOut, Nil, Nil, PromptStruct, 0, DataVerify) = True) Then
    Var mbOut as New MemoryBlock(DataVerify.Size)
    mbOut = DataVerify.pbData
    TextArea1.Text = TextArea1.Text + mbOut.CString(0) + EndOfLine
    System.DebugLog("Success")
  Else
    TextArea1.Text = TextArea1.Text + "Decryption Error"
    System.DebugLog("Error")
  End If
End Sub

```

```auto
Attributes( StructureAlignment = 4 ) Structure DATA_BLOB_32
  cbData as UInt32
  pbData as Ptr
End Structure

```

```auto
Attributes( StructureAlignment = 8 ) Structure DATA_BLOB_64
  cbData as UInt32
  pbData as Ptr
End Structure

```

```auto
Structure CRYPTPROTECT_PROMPTSTRUCT
  cbSize as UInt32
  dwPromptFlags as UInt32
  hwndApp as Integer
  szPrompt as WString
End Structure

```

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [March 9, 2024, 12:32pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/6 "2024-03-09T12:32:44Z")

</div>

Makes no sense to me how things are being handled.

I would assume that if you set StructureAlignment = 0, the current ABI would assume 4 or 8 automatically (depending on the target platform being compiled), so one structure and one declare should do the job for both 32 and 64 bit targets.

If it doesn’t, for me there’s a bug.

And that brings me another question, what’s the current alignment default? For me it should be 0 instead of 4.

---

<div class="post-metadata">

**Author:** ![Eugene\_Dakin](https://forum.xojo.com/user_avatar/forum.xojo.com/eugene_dakin/32/178_2.png) [@Eugene\_Dakin](https://forum.xojo.com/u/Eugene_Dakin)\
**Post date:** [March 9, 2024, 12:46pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/7 "2024-03-09T12:46:25Z")

</div>

> [@Rick\_Araujo](#):
>
> I would assume that if you set StructureAlignment = 0, the current ABI would assume 4 or 8 automatically (depending on the target platform being compiled), so one structure and one declare should do the job for both 32 and 64 bit targets.

Yes, you are correct that setting the StructureAlignment to zero does presume the correct length, and one declare does work for both 32-bit and 64-bit targets. Here is the revised code:

```auto
Sub Pressed() Handles Pressed
  //Works in 32-bit and 64-bit phase
  TextArea1.Text = ""
  
  Var pbDataInput as string = "Hello world of data protection."
  Var mbDataInput as New MemoryBlock(pbDataInput.Length+1)
  mbDataInput.CString(0) = pbDataInput
  
  Var DataIn as DATA_BLOB
  Var DataOut as DATA_BLOB
  Var DataVerify as DATA_BLOB
  
  TextArea1.Text = TextArea1.Text + "DataIn: " + DataIn.Size.ToString + " bytes" + EndOfLine
  Var dbDataInput as UInt32 
  dbDataInput = pbDataInput.Length + 1
  TextArea1.Text = TextArea1.Text + "pbDataInput: " + dbDataInput.ToString + EndOfLine
  TextArea1.Text = TextArea1.Text + "mbDataInput: " + mbDataInput.Size.ToString + EndOfLine
  DataIn.pbData = mbDataInput
  DataIn.cbData = mbDataInput.Size
  
  Var PromptStruct as CRYPTPROTECT_PROMPTSTRUCT
  PromptStruct.cbSize = PromptStruct.Size
  PromptStruct.dwPromptFlags = &h02
  PromptStruct.szPrompt = "This is a user prompt."
  
  //Begin protect phase
  Declare Function CryptProtectData Lib "Crypt32.dll" (ByRef pDataIn As Data_Blob, szDataDescr As WString, pOptionalEntropy As Ptr, pvReserved As Ptr, ByRef pPromptStruct As CRYPTPROTECT_PROMPTSTRUCT, dwFlags As UInt32, ByRef pDataOut As Data_Blob) As Boolean
  If (CryptProtectData(DataIn, "This is the description string.", Nil, Nil, PromptStruct, 0, DataOut) = True) Then
    TextArea1.Text = TextArea1.Text + "The encryption phase worked." + EndOfLine
    TextArea1.Text = TextArea1.Text + "DataOut " + DataOut.cbData.ToString + EndOfLine
  Else
    TextArea1.Text = TextArea1.Text + "Encryption Error"
  End If
  
  //Begin the unprotect phase
  TextArea1.Text = TextArea1.Text + "Begin the unprotect phase" + EndOfLine
  Declare Function CryptUnprotectData Lib "Crypt32.dll" (ByRef pDataIn As Data_Blob, ppszDataDescr As WString, pOptionalEntropy As Ptr, pvReserved As Ptr, ByRef pPromptStruct As CRYPTPROTECT_PROMPTSTRUCT, dwFlags As UInt32, ByRef pDataOut As Data_Blob) As Boolean
  
  Var pDescrOut as WString
  If (CryptUnprotectData(DataOut, pDescrOut, Nil, Nil, PromptStruct, 0, DataVerify) = True) Then
    Var mbOut as New MemoryBlock(DataVerify.Size)
    mbOut = DataVerify.pbData
    TextArea1.Text = TextArea1.Text + mbOut.CString(0) + EndOfLine
    System.DebugLog("Success")
  Else
    TextArea1.Text = TextArea1.Text + "Decryption Error"
    System.DebugLog("Error")
  End If
End Sub

```

```auto
Attributes( StructureAlignment = 0 ) Structure DATA_BLOB
  cbData as UInt32
  pbData as Ptr
End Structure

```

```auto
Structure CRYPTPROTECT_PROMPTSTRUCT
  cbSize as UInt32
  dwPromptFlags as UInt32
  hwndApp as Integer
  szPrompt as WString
End Structure

```

> [@Rick\_Araujo](#):
>
> And that brings me another question, what’s the current alignment default? For me it should be 0 instead of 4.

I agree that the alignment default _should_ be zero. If I remove the StructureAlignment value of 0, then I get an error.

Edit: Spelling

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [March 9, 2024, 12:49pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/8 "2024-03-09T12:49:41Z")

</div>

> [@Eugene\_Dakin](#):
>
> If I remove the StructureAlignment value of 0, then I get an error.

Curiously, what error? I would expect it acting wrong as before assuming 32 bit in a target 64.

Or am I bad interpreting what you intended to say?

---

<div class="post-metadata">

**Author:** ![Eugene\_Dakin](https://forum.xojo.com/user_avatar/forum.xojo.com/eugene_dakin/32/178_2.png) [@Eugene\_Dakin](https://forum.xojo.com/u/Eugene_Dakin)\
**Post date:** [March 9, 2024, 12:55pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/9 "2024-03-09T12:55:21Z")

</div>

> [@Rick\_Araujo](#):
>
> Curiously, what error? I would expect it acting wrong as before assuming 32 bit in a target 64.

The error only occurs in 64-bit mode. The error is:

**ERROR\_ACCESS\_DENIED**  
5 (0x5)  
Access is denied.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [March 9, 2024, 12:56pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/10 "2024-03-09T12:56:36Z")

</div>

Runtime error, Correct? Not compiler error.

---

<div class="post-metadata">

**Author:** ![Eugene\_Dakin](https://forum.xojo.com/user_avatar/forum.xojo.com/eugene_dakin/32/178_2.png) [@Eugene\_Dakin](https://forum.xojo.com/u/Eugene_Dakin)\
**Post date:** [March 9, 2024, 12:57pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/11 "2024-03-09T12:57:09Z")

</div>

Yes, you are correct that this is a runtime error.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [March 9, 2024, 1:01pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/12 "2024-03-09T13:01:48Z")

</div>

The 64 bit version got a smaller than expected structure (default as 32 bit we could infer), and trying to read regions outside the defined area raised the protection error. In the past it would just get some garbage and do random things. 🤣

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [March 9, 2024, 1:19pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/13 "2024-03-09T13:19:21Z")

</div>

> [@Eugene\_Dakin](#):
>
> Here is the revised code …
> 
> ```auto
> Attributes( StructureAlignment = 0 ) Structure DATA_BLOB
> cbData as UInt32
> pbData as Ptr
> End Structure
> 
> Structure CRYPTPROTECT_PROMPTSTRUCT
> cbSize as UInt32
> dwPromptFlags as UInt32
> hwndApp as Integer
> szPrompt as WString
> End Structure
> 
> ```

Wouldn’t be:

```auto
Attributes( StructureAlignment = 0 ) Structure DATA_BLOB
  cbData as UInt32
  pbData as Ptr
End Structure

Attributes( StructureAlignment = 0 ) Structure CRYPTPROTECT_PROMPTSTRUCT
  cbSize as UInt32
  dwPromptFlags as UInt32
  hwndApp as Integer
  szPrompt as WString
End Structure

```

?

---

<div class="post-metadata">

**Author:** ![Eugene\_Dakin](https://forum.xojo.com/user_avatar/forum.xojo.com/eugene_dakin/32/178_2.png) [@Eugene\_Dakin](https://forum.xojo.com/u/Eugene_Dakin)\
**Post date:** [March 9, 2024, 1:26pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/14 "2024-03-09T13:26:03Z")

</div>

I see where you are going with the structure by adding StructureAlignment = 0 for CRYPTPROTECT\_PROMPTSTRUCT. The program works in 32-bit and 64-bit mode with StructureAlignment = 0 or with it completely missing …

Edit: This is what makes structures confusing - In one instance StructureAlignment matters, and in another instance, it doesn’t matter.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [March 9, 2024, 2:57pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/15 "2024-03-09T14:57:49Z")

</div>

> [@Eugene\_Dakin](#):
>
> (In) CRYPTPROTECT\_PROMPTSTRUCT. The program works in 32-bit and 64-bit mode with StructureAlignment = 0 or with it completely missing …
> 
> Edit: This is what makes structures confusing - In one instance StructureAlignment matters, and in another instance, it doesn’t matter.

It always matters, but sometimes, due to a coincidence, a structure just fits into another alignment.

Let it assume the proper target ABI alignment and don’t run the risk of it not fitting.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [March 9, 2024, 3:14pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/16 "2024-03-09T15:14:20Z")

</div>

Observe the size and offsets changing with the alignment of the StrucDemo.

Aligned for 32 bit ABI:

 ![image](https://forum.xojo.com/uploads/default/original/3X/4/c/4c336c49761b3cfa04571898667f7ad5785f9bce.png)

Same structure aligned for 64 bit ABI:

 ![image](https://forum.xojo.com/uploads/default/original/3X/1/1/11bfe87be6f2c76e3bc3cd9af75877b1409e5bd2.png)

But this one, aligned to 32 or 64 bit, does not need paddings and fits without changes in both requirements, both contain 16 bytes in the same places:

 ![image](https://forum.xojo.com/uploads/default/original/3X/5/3/53b3a7951dd4142beae33fdc94b727dd8d2080a8.png)

 ![image](https://forum.xojo.com/uploads/default/original/3X/f/4/f4ef0004521298ff8f9271105880ccc095f1dfa7.png)

So just use align = 0 and let the compiler do it for you just changing the target.

---

<div class="post-metadata">

**Author:** ![Aurelian\_N](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/b4bc9f/32.png) [@Aurelian\_N](https://forum.xojo.com/u/Aurelian_N)\
**Post date:** [March 9, 2024, 5:07pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/18 "2024-03-09T17:07:14Z")

</div>

Hello Eugene, That is super nice, it seems to be a complete example.

I do have one question or request, i don’t want any notification for the user but i need this to be done on that pc only, so is there a way to do that ?

The idea is to Create a needed password with DPAPI, on that pc, store it in a file with the restriction that it can only be used on that PC, i guess that is the idea with DPAPI, and if i need it , i just pass the encrypted password to CryptUnprotectData and get it , i guess i could store it on the drive in a file as HexEncoded.

Thanks

---

<div class="post-metadata">

**Author:** ![Aurelian\_N](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/b4bc9f/32.png) [@Aurelian\_N](https://forum.xojo.com/u/Aurelian_N)\
**Post date:** [March 9, 2024, 5:16pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/19 "2024-03-09T17:16:50Z")

</div>

> [@Eugene\_Dakin](#):
>

From what i read it seems that i need to set 2 constants :

> Const CRYPTPROTECT\_UI\_FORBIDDEN = &h01  
> Const CRYPTPROTECT\_LOCAL\_MACHINE = &h04

and then i guess call the 2 methods with those 2 in .

---

<div class="post-metadata">

**Author:** ![Aurelian\_N](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/b4bc9f/32.png) [@Aurelian\_N](https://forum.xojo.com/u/Aurelian_N)\
**Post date:** [March 9, 2024, 5:59pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/20 "2024-03-09T17:59:38Z")

</div>

I guess this would do.

```auto

//Works in 32-bit and 64-bit phase
TextArea1.Text = ""

Var pbDataInput As String = "Hello world of data protection."
Var mbDataInput As New MemoryBlock(pbDataInput.Length+1)
mbDataInput.CString(0) = pbDataInput

#If Target32Bit
  Var DataIn As DATA_BLOB_32
  Var DataOut As DATA_BLOB_32
  Var DataVerify As DATA_BLOB_32
#Else //Target 64 bit
  Var DataIn As DATA_BLOB_64
  Var DataOut As DATA_BLOB_64
  Var DataVerify As DATA_BLOB_64
#EndIf

TextArea1.Text = TextArea1.Text + "DataIn: " + DataIn.Size.ToString + " bytes" + EndOfLine
Var dbDataInput As UInt32 
dbDataInput = pbDataInput.Length + 1
TextArea1.Text = TextArea1.Text + "pbDataInput: " + dbDataInput.ToString + EndOfLine
TextArea1.Text = TextArea1.Text + "mbDataInput: " + mbDataInput.Size.ToString + EndOfLine
DataIn.pbData = mbDataInput
DataIn.cbData = mbDataInput.Size

Var PromptStruct As CRYPTPROTECT_PROMPTSTRUCT
PromptStruct.cbSize = PromptStruct.Size
'PromptStruct.dwPromptFlags = &h01
PromptStruct.dwPromptFlags = &h02
PromptStruct.szPrompt = "This is a user prompt."

Var flags As UInt32 = CRYPTPROTECT_UI_FORBIDDEN + CRYPTPROTECT_LOCAL_MACHINE

//Begin protect phase
#If Target32Bit
  Declare Function CryptProtectData Lib "Crypt32.dll" (ByRef pDataIn As Data_Blob_32, szDataDescr As WString, pOptionalEntropy As Ptr, pvReserved As Ptr, pPromptStruct As Ptr, dwFlags As UInt32, ByRef pDataOut As Data_Blob_32) As Boolean
#Else //64 bit
  Declare Function CryptProtectData Lib "Crypt32.dll" (ByRef pDataIn As Data_Blob_64, szDataDescr As WString, pOptionalEntropy As Ptr, pvReserved As Ptr, pPromptStruct As Ptr, dwFlags As UInt32, ByRef pDataOut As Data_Blob_64) As Boolean
#EndIf

If (CryptProtectData(DataIn, "This is the description string.", Nil, Nil, Nil, flags, DataOut) = True) Then
  TextArea1.Text = TextArea1.Text + "The encryption phase worked." + EndOfLine
  TextArea1.Text = TextArea1.Text + "DataOut " + DataOut.cbData.ToString + EndOfLine
Else
  TextArea1.Text = TextArea1.Text + "Encryption Error"
End If

//Begin the unprotect phase
TextArea1.Text = TextArea1.Text + "Begin the unprotect phase" + EndOfLine

#If Target32Bit
  Declare Function CryptUnprotectData Lib "Crypt32.dll" (ByRef pDataIn As Data_Blob_32, ppszDataDescr As WString, pOptionalEntropy As Ptr, pvReserved As Ptr, pPromptStruct As Ptr, dwFlags As UInt32, ByRef pDataOut As Data_Blob_32) As Boolean
#Else //Target 64 bit
  Declare Function CryptUnprotectData Lib "Crypt32.dll" (ByRef pDataIn As Data_Blob_64, ppszDataDescr As WString, pOptionalEntropy As Ptr, pvReserved As Ptr, pPromptStruct As Ptr, dwFlags As UInt32, ByRef pDataOut As Data_Blob_64) As Boolean
#EndIf

Var pDescrOut As WString
If (CryptUnprotectData(DataOut, pDescrOut, Nil, Nil, Nil, flags, DataVerify) = True) Then
  Var mbOut As New MemoryBlock(DataVerify.Size)
  mbOut = DataVerify.pbData
  TextArea1.Text = TextArea1.Text + mbOut.CString(0) + EndOfLine
  System.DebugLog("Success")
Else
  TextArea1.Text = TextArea1.Text + "Decryption Error"
  System.DebugLog("Error")
End If

```

Thanks again

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [March 9, 2024, 6:39pm UTC](https://forum.xojo.com/t/dpapi-on-windows/79521/21 "2024-03-09T18:39:55Z")

</div>

> [@Aurelian\_N](#):
>
> ```auto
> #If Target32Bit
> Var DataIn As DATA_BLOB_32
> Var DataOut As DATA_BLOB_32
> Var DataVerify As DATA_BLOB_32
> #Else //Target 64 bit
> Var DataIn As DATA_BLOB_64
> Var DataOut As DATA_BLOB_64
> Var DataVerify As DATA_BLOB_64
> #EndIf
> 
> ```

The double approach is unnecessary as discussed. 😉

[Next page](https://forum.xojo.com/t/dpapi-on-windows/79521.md?page=2)
