[This is preliminary documentation and subject to change.]
CryptProtectData performs encryption on the data in a DATA_BLOB. Typically, only a user with the same login credential as the encrypter can decrypt the data. In addition, the encryption and decryption normally must be done on the same computer. See "Remarks" for information about exceptions.
BOOL WINAPI CryptProtectData(
DATA_BLOB *DataIn, // in
LPCWSTR szDataDescr, // in
DATA_BLOB *pOptionalEntropy, // in optional
PVOID *pvReserved, // reserved
CRYPTPROTECT_PROMPTSTRUCT *pPromptStruct, // in optional
DWORD dwFlags // in
DATA_BLOB *pDataOut; // out
);
The function returns TRUE if the function succeeded, FALSE if it failed. GetLastError returns the code for the cause of any failure. The pbData of the DATA_BLOB allocated must be freed using LocalFree.
Typically, only a user with login credentials matching those of the encrypter can decrypt the data. In addition, decryption normally can only be done on the computer where the data was encrypted. However, a user with a roaming profile may decrypt the data from another computer on the network.
If the CRYPTPROTECT_LOCAL_MACHINE dwFlag is set when the data is encrypted, any user on the machine where the encryption was done can decrypt the data.
The function creates a session key to perform the encryption. The session key is re-derived when the data is to be decrypted.
The function also adds a MAC (keyed integrity check) to the encrypted data to guard against data tampering.
France currently forbids this kind of encryption by law. In France, the Crypto API encryption call fails. The data protection function will ensure the integrity of the data but will not encrypt it.
// Encrypt data from DATA_BLOB Datain to DATA_BLOB DataOut.
// Then Decrypt to DATA_BLOB DataVerify.
// Declare and initialize variables
DATA_BLOB DataIn, DataOut, DataVerify;
BYTE *pbDataInput =(BYTE *)"Hello world of data protection.";
DWORD cbDataInput = strlen(pbDataInput)+1;
DataIn.pbData = pbDataInput;
DataIn.cbData = cbDataInput;
printf("the data to be encrypted is--- %s\n",pbDataInput);
CRYPTPROTECT_PROMPTSTRUCT PromptStruct;
ZeroMemory(&PromptStruct, sizeof(PromptStruct));
PromptStruct.cbSize = sizeof(PromptStruct);
PromptStruct.szPrompt = L"This is a user prompt.";
PromptStruct.dwPromptFlags = CRYPTPROTECT_PROMPT_ON_PROTECT;
printf("Begin protect phase\n");
if(!CryptProtectData(
&DataIn,
L"This is the description string.", // A description sting.
NULL, // Optional entropy not used
NULL, // Reserved
&PromptStruct, // Pass a promptstruct
0,
&DataOut)){
// The function failed. Report the error.
printf("Encryption error! errorcode=%lu \n",GetLastError());
return ;
}
printf("Begin Unprotect phase\n");
LPWSTR pDescrOut = (LPWSTR)0xbaadf00d ; // NULL;
if (!CryptUnprotectData(
&DataOut,
&pDescrOut,
NULL, // Optional Entropy,
NULL, // Reserved
&PromptStruct, // optional promptstruct
0,
&DataVerify)){
// The function faild. Report the error.
printf("Decryption error! errorcode=%lu\n", GetLastError());
return;
}
printf("The decrypted data is--%s\n", DataVerify.pbData);
printf("The description of the data was -- %s\n",pDescrOut);
LocalFree(pDescrOut);
// At this point, memcmp() could be used to compare DataIn.pbData and
// DataVerify.pbDate for equality. If the two functions worked
// correctly, the two byte strings should be identical.
LocalFree(DataOut.pbData);
LocalFree(DataVerify.pbData);
Windows NT: Requires version 5.0 or later.
Windows: Unsupported.
Windows CE: Unsupported.
Header: Declared in wincrypt.h.
Import Library: Use crypt32.lib.