I'm using CryptProtectData()
and CryptUnprotectData()
APIs for data encryption and decryption in my App.
Reading the API documentation, it's not clear why LocalFree()
needs to be called against the output buffer after usage. The example code on that page does not invoke LocalFree()
, is that a miss?
What's also missing in the documentation (the main reason for this question) is that, how is DATA_BLOB::pbData
for the output managed by DPAPI? Can I manage the memory for the output buffer myself? If I can, how do I know the output buffer size of the encrypted data in advance so that I can allocate a large enough buffer for CryptProtectData()
or CryptUnprotectData()
to use?
Here is a code snippet on how I'm using CryptProtectData()
:
DATA_BLOB dataIn;
DATA_BLOB dataOut;
dataIn.pbData = (BYTE *)"Hello world";
dataIn.cbData = (DWORD)strlen((char*)pbDataInput);
if(CryptProtectData(&dataIn, NULL, NULL, NULL, NULL, 0, &dataOut))
{
printf("Encrypted data size: %d", dataOut.cbData);
// LocalFree(dataOut.pbData); // Is this needed? Why? How do I manage dataOut.pbData by myself?
}