Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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



33.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)));


15.5k

It works, thank you so much.