Need some Help Using AES
Im trying to encrypt/decrypt with AES 256, but I can not get the same result as php, here is the example:
text = {"idServicio":79, "idProducto":209 , "referencia": "40425475190118187271", "montoPago": 9999, "telefono":"1111111111", "horaLocal":"20200401222821"}
Encrypted text = "DB8BRqb2q6BW/HLQYN2pr2n9DTL1Q8kp2lvZi3rogbxjEasiMbgU4q5/vDav+p0O0KWlfMmNekXN+UkbUiB+s/LNf1MF2EgOQEZoivxgp+UJxsuT5vDIMmQXUkuwkyUE+a7hH5FwaDDY8DNwW2kowFXeE69AcOMaWnyZ+YplKNEUOzQzLstBxWnJE+aSr0+vQN3knkIIjbT10yfSTV/OQA== "
PHP: class AesCipher { const CIPHER = 'AES-256-CBC'; const SECRET_KEY = 'HPo7OLqB4Fkk4E2yGOtwqw8H5fHR9kNx67OR4g4UdlA='; const SECRET_IV = 'p5ldmBPdd/9pjC0bDC/nSg==';
public static function encrypt($text)
{
$output = false;
$output = openssl_encrypt($text, self::CIPHER, base64_decode(self::SECRET_KEY), OPENSSL_RAW_DATA, base64_decode(self::SECRET_IV));
return base64_encode($output);
}
public static function decrypt($cypherText)
{
$output = false;
$output = openssl_decrypt(base64_decode($cypherText), self::CIPHER, base64_decode(self::SECRET_KEY), OPENSSL_RAW_DATA, base64_decode(self::SECRET_IV));
return $output;
}
}
Delphi: Lkey:=TBase64Encoding.Base64.DecodeStringToBytes( EditKey.Text); SKey:=''; for I := Low(Lkey) to High(Lkey) do SKey:=SKey + char(Lkey[i]);
Cipher.Init(Skey, {RawByteString(SkeyEditKey.Text)}
TFormat_HEX.Decode(RawByteString(EditInitVector.Text)),
StrToInt('0x' + EditFiller.Text));
I guess that the issue is when I convert key from base64, the IV is on hex.
Thanks
Update
I did a small change, and output test is similar, but no equal as PHP function, and I can't decrypt php text
var Skey:RawByteString;
SKey:=TFormat_Base64.Decode(EditKey.Text);
Cipher.Init(Skey,
TFormat_HEX.Decode(RawByteString(EditInitVector.Text)),
StrToInt('0x' + EditFiller.Text));
PHP Output:
DB8BRqb2q6BW/HLQYN2pr2n9DTL1Q8kp2lvZi3rogbxjEasiMbgU4q5/vDav+p0O0KWlfMm
NekXN+UkbUiB+s/LNf1MF2EgOQEZoivxgp+UJxsuT5vDIMmQXUkuwkyUE+a7hH5FwaDDY8D
NwW2kowFXeE69AcOMaWnyZ+YplKNEUOzQzLstBxWnJE+aSr0+vQN3knkIIjbT10yfSTV/OQA==
Delphi
DB8BRqb2q6BW/HLQYN2pr2n9DTL1Q8kp2lvZi3rogbxjEasiMbgU4q5/vDav+p0O0KWlfMm
NekXN+UkbUiB+s/LNf1MF2EgOQEZoivxgp+UJxsuT5vDIMmQXUkuwkyUE+a7hH5FwaDDY8D
NwW2kowFXeE69AcOMaWnyZ+YplKNEUOzQzLstBxWnJE+aSr0+v9Q21xA==
Have you called the Done method at the end in your Delphi application? That might be relevant for proper processing of the last data block.