I'm trying to call CryptProtectData from the Data Protection API (DPAPI):
DATA_BLOB dataIn;
DATA_BLOB dataEncrypted;
BYTE *phrase = (BYTE *)plain.c_str();
DWORD length = strlen((char *)phrase) + 1;
dataIn.pbData = phrase;
dataIn.cbData = length;
if(CryptProtectData(&dataIn, NULL, NULL, NULL, NULL,
CRYPTPROTECT_LOCAL_MACHINE, &dataEncrypted))
{
file.open(filePath, std::ios_base::binary);
if(!file.is_open())
{
return false;
}
file.write(reinterpret_cast<char *>(dataEncrypted.pbData),
dataEncrypted.cbData);
file.close();
LocalFree(dataEncrypted.pbData);
return true;
}
And I'm randomly getting the error:
Exception thrown at 0x77457E67 (ntdll.dll) in test.exe: 0xC00000005:
Access violation reading location 0x24146304
PFA
It's happening seemingly randomly on one machine (Windows Server2012), but seems to work fine on any other machine without error.
CRYPTPROTECT_LOCAL_MACHINE
, it can only be decrypted on the same machine that it was originally encrypted on. I ask because this sometimes is a common bug when using web-servers in a load-balancing arrangement; the other server happens to be called to service a request, and when it tries to decrypt the data: it can't - because it was originially encrypted on the "main" server.dataEncrypted.pbData
in case!file.is_open()