Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8819345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:20:06+00:00 2026-06-14T05:20:06+00:00

I’m trying to protect a local database that contains sensitive info (similar to this

  • 0

I’m trying to protect a local database that contains sensitive info (similar to this question, only for delphi 2010)

I’m using DISQLite component, which does support AES encryption, but I still need to protect this password I use to decrypt & read the database.

My initial idea was to generate a random password, store it using something like DPAPI (CryptProtectData and CryptUnprotectData functions found in Crypt32.dll), but I couldn’t find any example on that for Delphi

My question is: how can I safely store a randomly generated password? Or, assuming the DPAPI road is secure, how can I implement this DPAPI in Delphi?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:20:07+00:00Added an answer on June 14, 2026 at 5:20 am

    It’s better to use Windows’ DPAPI. It’s much more secure than using other methods:

    • CryptProtectData / CryptProtectMemory
    • CryptUnprotectData / CryptUnprotectMemory

    CryptProtectMemory / CryptUnprotectMemory offer more flexibility:

    • CRYPTPROTECTMEMORY_SAME_PROCESS: only your process can decrypt your data
    • CRYPTPROTECTMEMORY_CROSS_PROCESS: any process can dectypt your data
    • CRYPTPROTECTMEMORY_SAME_LOGON: only processes running with the same user and in the same session can decrypt data

    Pros:

    1. No need to have a key – Windows do it for you
    2. Granular control: per process / per session / per login / per machine
    3. CryptProtectData exists in Windows 2000 and newer
    4. DPAPI Windows is more secure than using "security" related code written from you, me and the people that believe Random() returns absolutely random number 🙂 In fact Microsoft has decades of experience in the security field, having the most attacked OS ever :o)

    Cons:

    1. In the case of CRYPTPROTECTMEMORY_SAME_PROCESS One* can just inject a new thread in your process and this thread can decrypt your data
    2. If someone reset user’s password (not change) you will be unable to decrypt your data
    3. In the case of CRYPTPROTECTMEMORY_SAME_LOGON: if the user* run hacked process it can decrypt your data
    4. If you use CRYPTPROTECT_LOCAL_MACHINE – every user* on that machine can decrypt the data. This is why it’s not recommended to save passwords in .RDP files
    5. Known issues

    Note: "every user" is a user who has tools or skills to use DPAPI

    Anyway – you have a choice.

    Note that @David-Heffernan is right – anything stored on the computer can be decrypted – reading it from memory, injecting threads in your process etc.

    On the other hand … why don’t we make cracker’s life harder? 🙂

    Rule of thumb: clear all buffers that contain sensitive data after using them. This doesn’t make things super safe, but decreases the possibility your memory to contain sensitive data.
    Of course this doesn’t solve the other major problem: how other Delphi components handle the sensitive data you pass to them 🙂

    Security Library by JEDI has object oriented approach to DPAPI. Also JEDI project contains translated windows headers for DPAPI (JWA IIRC)

    UPDATE: Here’s sample code that uses DPAPI (using JEDI API):

    Uses SysUtils, jwaWinCrypt, jwaWinBase, jwaWinType;
    
    function dpApiProtectData(var fpDataIn: tBytes): tBytes;
    var
      dataIn,               // Input buffer (clear-text/data)
      dataOut: DATA_BLOB;   // Output buffer (encrypted)
    begin
      // Initializing variables
      dataOut.cbData := 0;
      dataOut.pbData := nil;
    
      dataIn.cbData := length(fpDataIn); // How much data (in bytes) we want to encrypt
      dataIn.pbData := @fpDataIn[0];     // Pointer to the data itself - the address of the first element of the input byte array
    
      if not CryptProtectData(@dataIn, nil, nil, nil, nil, 0, @dataOut) then
        RaiseLastOSError; // Bad things happen sometimes
    
      // Copy the encrypted bytes to RESULT variable
      setLength(result, dataOut.cbData);
      move(dataOut.pbData^, result[0], dataOut.cbData);
      LocalFree(HLOCAL(dataOut.pbData));                  // http://msdn.microsoft.com/en-us/library/windows/desktop/aa380261(v=vs.85).aspx
    //  fillChar(fpDataIn[0], length(fpDataIn), #0);  // Eventually erase input buffer i.e. not to leave sensitive data in memory
    end;
    
    function dpApiUnprotectData(fpDataIn: tBytes): tBytes;
    var
      dataIn,               // Input buffer (clear-text/data)
      dataOut: DATA_BLOB;   // Output buffer (encrypted)
    begin
      dataOut.cbData := 0;
      dataOut.pbData := nil;
    
      dataIn.cbData := length(fpDataIn);
      dataIn.pbData := @fpDataIn[0];
    
      if not CryptUnprotectData(
        @dataIn,  
        nil, 
        nil, 
        nil, 
        nil, 
        0,         // Possible flags: http://msdn.microsoft.com/en-us/library/windows/desktop/aa380261%28v=vs.85%29.aspx 
                   // 0 (zero) means only the user that encrypted the data will be able to decrypt it
        @dataOut
      ) then
        RaiseLastOSError;
    
      setLength(result, dataOut.cbData);                  // Copy decrypted bytes in the RESULT variable
      move(dataOut.pbData^, result[0], dataOut.cbData);   
      LocalFree(HLOCAL(dataOut.pbData));                  // http://msdn.microsoft.com/en-us/library/windows/desktop/aa380882%28v=vs.85%29.aspx
    end;
    
    procedure testDpApi;
    var
      bytesClearTextIn,       // Holds input bytes
      bytesClearTextOut,      // Holds output bytes
      bytesEncrypted: tBytes; // Holds the resulting encrypted bytes
      strIn, strOut: string;  // Input / Output strings
    begin
    
      // *** ENCRYPT STRING TO BYTE ARRAY
      strIn := 'Some Secret Data Here';
    
      // Copy string contents to bytesClearTextIn
      // NB: this works for STRING type only!!! (AnsiString / UnicodeString)
      setLength(bytesClearTextIn, length(strIn) * sizeOf(char));
      move(strIn[1], bytesClearTextIn[0], length(strIn) * sizeOf(char));
    
      bytesEncrypted := dpApiProtectData(bytesClearTextIn);     // Encrypt data
    
      // *** DECRYPT BYTE ARRAY TO STRING
      bytesClearTextOut := dpApiUnprotectData(bytesEncrypted);  // Decrypt data
    
      // Copy decrypted bytes (bytesClearTextOut) to the output string variable
      // NB: this works for STRING type only!!! (AnsiString / UnicodeString)    
      setLength(strOut, length(bytesClearTextOut) div sizeOf(char));
      move(bytesClearTextOut[0], strOut[1], length(bytesClearTextOut));
    
      assert(strOut = strIn, 'Boom!');  // Boom should never booom :)
    
    end;
    

    Notes:

    • The example is lightweight version of using CryptProtectData / CryptUnprotectData;
    • Encryption is byte oriented so it’s easier to use tBytes (tBytes = array of byte);
    • If input and output string are UTF8String, then remove "* sizeOf(char)", because UTF8String’s char is 1 byte only
    • The use of CryptProtectMemory / CryptUnProtectMemory is similar
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.