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.

A bug of Phalcon\Crypt.decrypt in 2.0

$crypt = new Phalcon\Crypt();

$key = 'le password'; $text = 'This is a secret text';

$encrypted = $crypt->encrypt($text, $key);

echo $crypt->decrypt($encrypted, $key);

The last line will always throw an error: Parameter 'text' must be a string.



7.2k
Accepted
answer
edited Mar '15

Your key is too short.

your key shoud be 16, 24 or 32 charcters.

change your key like:

$key = 'le password12345'; // 16 characters


3.0k

Thanks, it works now, however same code works very well in 1.3.4.

Currently, crypt throws an incomprehensible msg in 2.0, it might be good to show some appropriate msgs, like 'the key is too short~~'

Your key is too short.

your key shoud be 16, 24 or 32 charcters.

change your key like:

```php $key = 'le password12345'; // 16 characters ```

If you set your php configuration to development values. like setting Error Reporting, .... the code give you the message that you want. this is the exeption:

PHP Warning:  mcrypt_encrypt(): Key of size 11 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in /home/aboozar/www/zephir/test.php on line 4
PHP Stack trace:
PHP   1. {main}() /home/aboozar/www/zephir/test.php:0
PHP   2. Phalcon\Crypt->encrypt() /home/aboozar/www/zephir/test.php:4
PHP   3. mcrypt_encrypt() /home/aboozar/www/zephir/test.php:4
PHP Warning:  mcrypt_decrypt(): Key of size 11 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in /home/aboozar/www/zephir/test.php on line 5
PHP Stack trace:
PHP   1. {main}() /home/aboozar/www/zephir/test.php:0
PHP   2. Phalcon\Crypt->decrypt() /home/aboozar/www/zephir/test.php:5
PHP   3. mcrypt_decrypt() /home/aboozar/www/zephir/test.php:5
PHP Fatal error:  Uncaught exception 'InvalidArgumentException' with message 'Parameter 'text' must be a string' in /home/aboozar/www/zephir/test.php:5
Stack trace:
#0 [internal function]: Phalcon\Crypt->_cryptUnpadText(false, 'cbc', 32, 0)
#1 /home/aboozar/www/zephir/test.php(5): Phalcon\Crypt->decrypt('\xF8=J\xB0?\xA1\x8BWF\xD4r\xC9\xFC\r<...', 'le password')
#2 {main}
  thrown in /home/aboozar/www/zephir/test.php on line 5