Cakephp Invalid key for decrypt(), key must be at least 256 bits (32 bytes) long: Solved

In cakephp 3 I tried to set cookie in my controller using write cookie method. Error got invalid key for decrypt(), key must be at least 256 bits (32 bytes) long – used following line of code.

$this->Cookie->write(‘username’, ‘test’, true, ‘1 year’);

I got following error after execting page.
Invalid key for decrypt(), key must be at least 256 bits (32 bytes) long

Before using cookie write method, I loaded cookie component in appcontroller using following line.
$this->loadComponent(‘Cookie’);

I loaded conponnet in initialize method (appcontroller.php)

After searching I found following solution. I changed app.php file and added following line of code

'Security' => [
        'salt' => '123456789purab@eparinay!@#*$($(',
    ]

Above solution worked for me and it solved issue.

If you are getting this error means your Security.salt in app.php does not have the correct length.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *