I'm having trouble encrypting and decrypting some information with the DCP Crypt library. I'm using Delphi XE and the functions posted at PHP to Delphi and back Encryption-Decryption using Rijndael, but it doesn't work.
I think my problem is because the key I'm using is in hex mode instead of binary.
var
Key, IV: ansiString;
Data: ansiString;
begin
Key := '09CB0785F13CD0D557C0940E72E0DCDC86CDC89769044E95DB51A782E7D996FFF3';
Iv := '09CB0785F13CD0D557C0940E72E0DCDC';
Data := <?xml version="1.0" encoding="UTF-8"?><MyNode><Head><CustNo>...';
When I use the EncryptData
function, must I pass the public key in binary or in hex?
asCryptBody := EncryptData(asXMLBody, Public_Key, Init_vector);
or
asCryptBody := EncryptData(asXMLBody, HexToStrBin(Public_Key), HexToStrBin(Init_vector));
where
function StrBinToHex(const s: AnsiString): AnsiString;
var
i:integer;
begin
result := '';
for i := 1 to length(s) do
result := result + inttohex(Ord(s[i]),1);
end;
function HexToStrBin(const s: AnsiString): AnsiString;
var
i:integer;
begin
result := '';
for i := 1 to (length(s) div 2) do
result := result + char( strtoint('$'+ copy(s,(i*2)-1,2)) );
end;
I've tried two ways, but the encryption still differs from PHP encryption.
Maybe the functions HexToStrBin and StrBinToHex are the problem?