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?


$crypto = new Phalcon\Crypt();
$data = bin2hex($crypto->encrypt("1:10000:0:100"));


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

and html shows

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


It works, thank you so much.