Hi,
I have a database and there is a field of data that is encrypted using the
CryptProtectData function. Once encrypted, the data is in binary format.
I tried to use the following functions I wrote (heavily bastardized from somebody's old Delphi source) to decrypt this data:
function BlobDataToHexStr(P: PByte; I: Integer): string;
var
HexStr: string;
begin
HexStr := '';
while (I > 0) do begin
Dec(I);
HexStr := HexStr + IntToHex(P^, 2);
Inc(P);
end;
Result := HexStr;
end;
function DecryptData(sData: string): string;
var
DataIn: DATA_BLOB;
DataOut: DATA_BLOB;
DataHash: string;
begin
DataOut.cbData := 0;
DataOut.pbData := nil;
DataIn.pbData := Pointer(WideString(sData));
DataIn.cbData := Length(sData) * SizeOf(WChar);
CryptUnprotectData(@DataIn, nil, nil, nil, nil, CRYPTPROTECT_UI_FORBIDDEN, @DataOut);
DataHash := BlobDataToHexStr(DataOut.pbData, DataOut.cbData);
Result := DataHash;
LocalFree(Cardinal(DataOut.pbData));
LocalFree(Cardinal(DataIn.pbData));
end;
I then called the
DecryptData function with the following code:
while not Query.Eof do
begin
sEncrData := 'Encrypted Data: ' + Query.FieldByName('data_value').AsString + #13#10;
sDecrData := 'Decrypted Data: ' + DecryptData(Query.FieldByName('data_value').AsString) +#13#10;
End;
When I write
sEncrData, as expected, data that looks like this is returned:
ÑŒz ÀO—ë 0@ [etc.]
However, when I write
sDecrData nothing is returned.
If somebody can shed any light on this, it would be very much appreciated!