DPAPI On Windows

ok, i have a dilema here, while this works as a whole and indeed double approach is not needed, i try to have this in 2 separate functions . One to Encrypt the string and store it in a file and one to decrypt it from the file.

Apparently saving the DataOut.pbData into a MemoryBlock it shows it properly but when trying to extract that as MemoryBlock.StringValue it comes out empty , so any idea why ?

Is there another approach that i should follow ?

Thanks

Seems to work for me:

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
  Var bs As BinaryStream = BinaryStream.Create(SpecialFolder.Desktop.Child("output.bin"))
  Var mb As MemoryBlock = DataOut.pbData
  bs.Write(mb.StringValue(0, DataOut.cbData))
  bs.Close

Hello @Aurelian_N,

Building on Andrews helpful information, here is code that will take text from a Textfield called TFDataToEncrypt and save it in encrypted form into a binary file on the desktop:

Sub Pressed() Handles Pressed
  //Works in 32-bit and 64-bit phase
  //Start the encrypt phase
  
  Var pbDataInput as string = TFDataToEncrypt.Text //Text to be encrypted
  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
  
  Var dbDataInput as UInt32 
  dbDataInput = pbDataInput.Length + 1
  DataIn.pbData = mbDataInput
  DataIn.cbData = mbDataInput.Size
  
  Var PromptStruct as CRYPTPROTECT_PROMPTSTRUCT
  PromptStruct.cbSize = PromptStruct.Size
  PromptStruct.dwPromptFlags = CRYPTPROTECT_PROMPT_ON_PROTECT
  PromptStruct.szPrompt = "This is a user prompt."
  
  //Begin protect phase
  If (CryptProtectData(DataIn, "This is the description string.", Nil, Nil, PromptStruct, 0, DataOut) = True) Then
    //Encryption was successful
    //Save encrypted information in a binary file called 'EncryptedFile.bin'
    Var f as FolderItem = SpecialFolder.Desktop.Child("EncryptedFile.bin")
    //Check if an existing file exists
    If f.Exists Then
      f.Remove
    End If
    var bs as BinaryStream = BinaryStream.Create(SpecialFolder.Desktop.Child("EncryptedFile.bin"))
    Var mb as MemoryBlock = DataOut.pbData
    bs.Write(mb.StringValue(0, DataOut.cbData))
    bs.Close
  Else
    MessageDialog.Show("BtnEncrypt Error")
  End If
End Sub

Here is the code to extract the encrypted information from the binary file and show the data in a MessageDialog box:

Sub Pressed() Handles Pressed
  //Begin the unprotect phase
  Var f as FolderItem = SpecialFolder.Desktop.Child("EncryptedFile.bin")
  If (f <> Nil) and f.Exists Then
    Var rs as BinaryStream = BinaryStream.Open(f, False)
    rs.LittleEndian = True
    Var mb as MemoryBlock = rs.Read(rs.Length)
    Var DataOut as DATA_BLOB
    DataOut.pbData = mb
    DataOut.cbData = mb.Size
    Var DataVerify as DATA_BLOB
    
    Var PromptStruct as CRYPTPROTECT_PROMPTSTRUCT
    PromptStruct.cbSize = PromptStruct.Size
    PromptStruct.dwPromptFlags = CRYPTPROTECT_PROMPT_ON_PROTECT
    PromptStruct.szPrompt = "This is a user prompt."
    
    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
      //Show the decrypted inforation
      MessageDialog.Show("Secret Info: " + mbOut.CString(0))
    Else
      MessageDialog.Show("BtnDecrypt Error")
    End If
  End If
End Sub

Many thanks to the participants in this thread to help Aurelian with his question :slight_smile:

Nice, that works smooth, initially i stored the data as HexEncoded so i can see it and then i process that eventually , but this is a full working path.

Thanks again.

1 Like