How to get the size of encrypted data?

I have a difficult question, and I hope I can explain it. I want to read the value from the Windows registry, which is saved by another program that I don’t have a source, but I already know the type of this value and its like this:

  _MyData = record
    byteType: Byte;
    encData: PByte;
  end;

byteType indicates the type of this data is integer (1,2,3 ...), you can forget about this parameter, whereas encData is encrypted data using the crypt32.dll windows function (CryptProtectData) I use the following code to read the value from registry:

procedure TForm1.Button2Click(Sender: TObject);
var
  myData: _MyData;
  reg: TRegistry;
  valueSize: Integer;
begin
  reg := TRegistry.Create;
  try
    if reg.OpenKey(KEY_PATH,false) then
      Begin
        valueSize := reg.GetDataSize(VALUE_NAME);
        reg.ReadBinaryData(VALUE_NAME, myData, valueSize);
      End;
  finally
    reg.Free;
  end;
end;

// KEY_PATH, VALUE_NAME are string constants.

So now I have the encrypted data in myData.encData, and now I want to decrypt it by passing it the CryptUnprotectData function, which has this signature:

function CryptUnprotectData(pDataIn: PDATA_BLOB; ppszDataDescr: PLPWSTR; pOptionalEntropy: PDATA_BLOB; pvReserved: Pointer; pPromptStruct: PCRYPTPROTECT_PROMPTSTRUCT; dwFlags: DWORD; pDataOut: PDATA_BLOB): BOOL; stdcall;

DATA_BLOB, :

  _CRYPTOAPI_BLOB = record
    cbData: DWORD;
    pbData: PBYTE;
  end;
  DATA_BLOB = _CRYPTOAPI_BLOB;
  PDATA_BLOB = ^DATA_BLOB;

pbData - ( ), cbData - , (I ) myData.encData, PByte, , ? CryptUnprotectData , outpout, , ?

.

: ,

  _MyData = packed record
    byteType: Byte;
    encData: array of byte;
  end;

procedure TForm1.Button2Click(Sender: TObject);
var
  myData: ^_MyData;
  reg: TRegistry;
  valueSize: Integer;
  dataIn, dataOut: DATA_BLOB;
begin
  reg := TRegistry.Create;
  try
    if reg.OpenKey(KEY_PATH,false) then
      Begin
        valueSize := reg.GetDataSize(VALUE_NAME);
        GetMem(myData, ValueSize);
        try
          reg.ReadBinaryData(VALUE_NAME, myData^, valueSize);

          dataOut.cbData := 0;
          dataOut.pbData := nil;
          dataIn.cbData := Valuesize - SizeOf(Byte);
          dataIn.pbData := @myData.encData;
          CryptUnprotectData(@dataIn,nil,nil,nil,nil,CRYPTPROTECT_UI_FORBIDDEN,@dataOut);

          //yes, it works, Thank you very much Ken Bourassa


        finally
          FreeMem(myData);
        End;
      End;
  finally
    reg.Free;
  end;

end;
+3
2

reg.GetDataSize - SizeOf (Byte)

,

_MyData 8 . ,

reg.ReadBinaryData(VALUE_NAME, myData, valueSize); 

, 8 , . 8 , EncData .

:

_MyData = packed record              
  byteType: Byte;              
  encData: array[0..MaxInt] of byte;            
end    

procedure TForm1.Button2Click(Sender: TObject);    
var      
  myData: ^_MyData;
  reg: TRegistry;      
  valueSize: Integer;    
begin      
  reg := TRegistry.Create;      
  try        
    if reg.OpenKey(KEY_PATH,false) then          
    Begin            
      valueSize := reg.GetDataSize(VALUE_NAME);            
      GetMem(MyData, ValueSize);
      try
        reg.ReadBinaryData(VALUE_NAME, myData^, valueSize); 
        //Do what is needed with MyData. The size of MyData.EncData = Valuesize - SizeOf(Byte)
      finally
        FreeMem(MyData);
      end;

    End;      
  finally        
    reg.Free;      
  end;    
end;

packed , , ... , . EncData Array[0..MaxInt] of byte. _MyData, , , . Range Checking = False , ( , ), Array[0..0] of byte. , , , .

EDIT ( ):

, EncData Array of byte , PByte. Array of byte ( 4 ). EncByte [0] , , , AV. , , , @myData.encData ( , GetMem). , , EncData :

_MyData = packed record              
  byteType: Byte;              
  encData: Record end;            
end    

, , EncData, , .

+4

, - "PByte". ? , , ( , ). , ? , , - ValueSize - SizeOf (Byte), "" , .

+3

All Articles