Hello,
I've to process with Lazarus a CSV file created by PHP with an encrypted field. I use Lazarus for years, but I'm not a php programmer (I just have some basic knowledge of PHP).
With Lazarus I already have experience with DCP, which I was currently using with RC4 / SHA1, 3DES / MD5 and more. However I can't find a way to encode a string with PHP that is decryptable with Lazarus, or even with OpenSSL from the terminal (Linux).
On PHP I use a function that works fine with AES-256, I checked by decrypting and getting the original string.
With Lazarus I tried to use DCPrijndael, which I had not used yet, and I can encrypt / decrypt a string correctly, if I don't use initial vector, while if I use an Iv the decrypt differs in two characters with the original string (the other characters are correct).
However PHP and Lazarus seem like separate worlds and I haven't found a way to encrypt on PHP that makes it possible to decrypt with Lazarus. I've also tried to crypt with RC4 / SHA1, that I already use with Lazarus, but PHP and Lazarus seems work differently.
Here is my PHP code:
//PHP CODE
function encrypt_decrypt($action, $string)
{
/* =================================================
* ENCRYPTION-DECRYPTION
* =================================================
* ENCRYPTION: encrypt_decrypt('encrypt', $string);
* DECRYPTION: encrypt_decrypt('decrypt', $string) ;
* =================================================
Make sure to create 32 byte secret_key and 16 byte secret_iv
* =================================================
*/
$output = false;
$encrypt_method = "AES-256-CBC";
$secret_key = '12345678901234567890123456789012';
$secret_iv = 'mystring to convert in HEX'; //dca12345678901a3 (HEX --> $iv)
// hash
$key = hash('sha256', $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
if ($action == 'encrypt') {
$output = base64_encode(openssl_encrypt($string, $encrypt_method, $key, 0, $iv));
} else {
if ($action == 'decrypt') {
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
}
return $output;
}
Here my Lazarus encrypt code:
uses
DCPrijndael, DCPsha1, DCPsha256, Base64, DCPcrypt2
var
S, T, Z, MyKey, MyIv: AnsiString;
MyDCP_rijndael: TDCP_rijndael;
//Encrypt:
MyDCP_rijndael := TDCP_rijndael.Create (nil);
S := 'Original string to encrypt';
MyKey := '12345678901234567890123456789012'; //Password 32 char
MyIv := 'dca12345678901a3'; //16 char HEX
try
MyDCP_rijndael.InitStr(MyKey, TDCP_sha256);
//MyDCP_rijndael.SetIV(MyIv); //Commented, because sith Iv 2 characters of the string are different with original after crypt and decrypt
//These 2 rows are commented because the result is the same:
//MyDCP_rijndael.BlockSize := Length(MyIv); //Should this be also 128? --> It is the same if 128 or Length(MyIv)
//MyDCP_rijndael.CipherMode := cmCBC; //Requires DCPcrypt2 in the uses, else error Identifier not found "cmCBC"
MyDCP_rijndael.EncryptCBC(S[1],S[1],Length(S));
MyDCP_rijndael.Burn;
Z := EncodeStringBase64(S);
except
Z := 'ERROR';
end;
Clipboard.AsText := Z;
showmessage (Z);
MyDCP_rijndael.Free;
and here my Lazarus decrypt code:
uses
DCPrijndael, DCPsha1, DCPsha256, Base64, DCPcrypt2
var
S, T, Z, MyKey, MyIv: AnsiString;
MyDCP_rijndael: TDCP_rijndael;
//Decrypt:
MyDCP_rijndael := TDCP_rijndael.Create (nil);
S := 'Original string to decrypt';
T := DecodeStringBase64(S);
MyKey := '12345678901234567890123456789012'; //Password 32 char
MyIv := 'dca12345678901a3'; //16 char HEX
try
MyDCP_rijndael.InitStr(MyKey, TDCP_sha256);
//MyDCP_rijndael.SetIV(MyIv); //Commented, because sith Iv 2 characters of the string are different with original after crypt and decrypt
//These 2 rows are commented because the result is the same:
//MyDCP_rijndael.BlockSize := Length(MyIv); //Should this be also 128? --> It the same if 128 or Length(MyIv)
//MyDCP_rijndael.CipherMode := cmCBC; //Requires DCPcrypt2 in the uses, else error Identifier not found "cmCBC"
MyDCP_rijndael.DecryptCBC(T[1],T[1],Length(T));
MyDCP_rijndael.Burn;
Z := T;
except
Z := 'ERROR';
end;
Clipboard.AsText := Z;
showmessage (Z);
MyDCP_rijndael.Free;
I can choose any other crypt algorithm, the important thing is that it can encrypt with PHP and decrypt with Lazarus.
Thanks for any help.
Best regards,
Stephanie