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?
It’s better to use Windows’ DPAPI. It’s much more secure than using other methods:
CryptProtectMemory / CryptUnprotectMemory offer more flexibility:
Pros:
Cons:
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):
Notes: