We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

What's the problem with my encrypt and decrypt code?

encrypt

$crypto = new Phalcon\Crypt();
$crypto->setCipher('rijndael-128');
$crypto->setKey($secure_key);
$data = bin2hex($crypto->encrypt("1:10000:0:100"));

decrypt

$data = $this->crypto->decrypt(hex2bin($data));

and html shows



34.6k
Accepted
answer
edited May '15

The extra characters are there because of padding. Block cipher modes for symmetric-key encryption algorithms in ECB/CBC modes require plain text input that is a multiple of the block size, so messages must be padded to bring them to this length when they're decripted. This is why you find extra zero null characters at the end of the input causing both strings not to be the same.

You can trim these characters after decryption:

$data = rtrim($this->crypto->decrypt(hex2bin($data)));


29.1k

It works, thank you so much.