I try to decrypt a string witch is encrypted with PHP. I am Using Lazarus, and DCPCrypt under Linux.
The PHP Code for encrypting looks like:
if(!function_exists("my_encrypt")) {
function my_encrypt($data, $key)
{
// Remove the base64 encoding from our key
$encryption_key = base64_decode($key);
// Generate an initialization vector
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('AES-256-CBC'));
// Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.
$encrypted = openssl_encrypt($data, 'AES-256-CBC', $encryption_key, 0, $iv);
// The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)
return base64_encode($encrypted . '::' . $iv);
}
}
The $key variable is a constant and is known in Lazarus
The IV (initialisation Vektor) is concatenated after the encrypted string, separated by ::
The encrypted string is in a mysql database and read out via ZEOS
Below the code for decryption:
function my_decrypt(data:string; key:string) : String;
var
index, dataLength, bsize, pad: integer;
datarr : TArrayOfString;
Cipher : TDCP_rijndael;
decryptedstr : String;
DataString, encryption_key, IV : AnsiString;
begin
datarr := SplitString('::', DecodeStringBase64(data), 2);
encryption_key := DecodeStringBase64(key);
IV := datarr[1];
DataString := datarr[0];
Cipher := TDCP_rijndael.Create(nil);
Cipher.Init(encryption_key[1],Length(encryption_key)*8,@IV[1]);
Cipher.CipherMode:= cmCBC;
Cipher.DecryptCBC(DataString[1],DataString[1],Length(DataString));
my_decrypt := DataString;
Cipher.Free;
end;
Check In Lazarus gives the right length for Decrypted string, IV and key, however the decrypted string is wrong and not readable.
Decryption in PHP works fine.
Some having ideas?