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

Crypt gives different encrypted values for same source-text

Hi,

I'm using Phalcon\Crypt to encode URL-parameter-information that should not be visible to the public. Everything works just fine (using encryptBase64/decryptBase64), just one thing bugs me: Every time I encrypt a source-text, the encrypted value will be something else, e.g. encrypted the word "test" will have totally different encrypted values. Even though the DEcryption works all the time, the project requires to have unique URL-parameters to work for RSS-feeds and third-party applications. So, if the encrypted value is "12345" for the source-text "test", it should always be "12345" and not "23456" the next time I'm encrypting the source-text, so my URLs, containing the encrypted value, will stay the same and remain unique.

Is there a way to accomplish this? My idea for a workaround would be to cache all encrypted values and check if a value has been encrypted before encryption happens again and get the cached value from the database - but this seems way too much overhead for a (hopefully) simple issue.

I don't know, if this matters, but Crypt is written to the DI like this:

$di->set(
    'crypt',
    function () use ($config) {
        $crypt = new Crypt();
        $crypt->setMode(MCRYPT_MODE_CFB);
        $crypt->setKey($config->cryptKey);
        return $crypt;
    }
);

Any hint is appreciated. Thanks, Dirk

Could you please post a small script that allow us to reproduce the issues?

edited Aug '15

sure:

$crypt = new Crypt();
$crypt->setMode(MCRYPT_MODE_CFB);
$crypt->setKey('secret');
var_dump($crypt->encryptBase64('text'));
var_dump($crypt->encryptBase64('text'));

The outcome are two different encrypted values, which can be decrypted to "text" all right, but I'd expect the encrypted values to be the same to be able to create unique URLs that don't change all the time.