120

I installed a new fresh copy of Laravel 5.3 using composer but I'm getting this error:

The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths. Even though my app.php file in config directory specify
'cipher' => 'AES-128-CBC'

3
  • Refer to this github [issue][1].
    – LF00
    Aug 17, 2017 at 2:51
  • 1
    i have this issue and it is totally random... 90% of my requests go through without any errors, then i get one HTTP 500 with this in the logs. May 19, 2018 at 12:05
  • If it is a fresh install, then create the symlink of storage in public directory with this command: php artisan storage:link Sep 3, 2020 at 5:47

9 Answers 9

159

You need to have .env on your appication folder then run:

$ php artisan key:generate

If you don't have .env copy from .env.example:

$ cp .env.example .env
8
  • 3
    I am also having this exact problem on a fresh install. I've run the key generator, cleared and re-cached. I checked the .env file and the key was successfully inserted but the error persists.
    – Rottingham
    Nov 20, 2016 at 21:23
  • @Rottingham, in your config/app.php what do you have as 'key' and 'cipher'? Nov 21, 2016 at 11:16
  • 3
    I had the same, AES-256-CBC as the ciper, and the KEY was env('APP_KEY'); I discovered my problem was key:generate was concatenating they key in the .env file instead of replacing it.
    – Rottingham
    Nov 23, 2016 at 1:59
  • 1
    @CodeCanyon have you tried checking key length as suggested by stackoverflow.com/a/40709643/1008916? Jan 10, 2017 at 12:53
  • 1
    "Just run these commands" is not a very good answer. While it may solve the problem, it offers no deeper understanding of what, exactly is being done. Nor does it answer whether these values should be the same on a production server versus a development server versus one's workstation.
    – S. Imp
    Feb 22, 2017 at 5:38
28

Run this commands on your terminal:

php artisan config:clear
then
php artisan config:cache

2
  • 2
    Just as a P.S. or FYI - running config:cache automatically runs config:clear as you see in the terminal when running config:cache
    – Rottingham
    Nov 20, 2016 at 21:22
  • 1
    I had a .env file and a APP_KEY value, this works in my case. Thanks.
    – Setar
    Dec 20, 2016 at 14:50
19

Ok, this has basically already been answered, but I found a couple caveats that had be consternated, or constipated, one of those two...

First, as has already been said, you should ensure you have a valid .env file which you can accomplish in the terminal by copying the existing .env.example file as such:

$ cp .env.example .env

Then, generate your application Key

$ php artisan key:generate

Once this is done, make sure to open your .env file and ensure that the APP_KEY line looks correct - this is where my consternation came from:

APP_KEY=base64:MsUJo+qAhIVGPx52r1mbxCYn5YbWtCx8FQ7pTaHEvRo=base64:Ign7MpdXw4FMI5ai7SXXiU2vbraqhyEK1NniKPNJKGY=

You'll notice that the key length is wrong, it for some unknown reason (probably from running key:generate multiple times) has two base64= keys in there. Removing one is the fix to the problems I was having and this appears to be an Artisan/Laravel bug.

Hope this answer helps anyone who may be struggling with the same problems or annoying bug.

5
  • I'm facing this issue as well, but it only pops up every now and then. Key is correct, so I assume there's some other problem going on. I've done the config caching, suggested by Lee, and will see how it goes. For reference, I have a whole bunch of other access tokens in the file, so one of them could be causing an issue. However, if they were, I'd expect it to happen every time. (L5.4) Feb 28, 2017 at 19:01
  • 1
    This, plus doing php artisan config:clear then php artisan config:cache, fixed my installation.
    – zundi
    Mar 19, 2017 at 21:40
  • @MikeRockétt There is discussion here: github.com/laravel/framework/issues/9080 regarding multiple ajax calls pulling the app_key at the same time and .env file being locked from the first call. Recommended fix is to not call the .env in production but using the cache?
    – nwolybug
    Feb 15, 2018 at 16:15
  • That's it !!! Fixed for me. I got two keys! Removed one and worked :)
    – saulob
    Feb 12, 2021 at 13:48
  • your text in bold helped me. thanks Jan 20 at 13:24
12

in .env file give this key and you are done

APP_KEY=ABCDEF123ERD456EABCDEF123ERD456E

Still not working?

If you are working from cli, reboot the server and it will.

Want explanation?

ok, as the error message says:

The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.

Key length for AES-128-CBC is 16 characters e.g ABCDEF123ERD456E

Key length for AES-256-CBC is 32 characters e.g ABCDEF123ERD456EABCDEF123ERD456E

Make sure in config/app.php the cipher is set to the appropriate cipher like the two above and the key is pointing to the .env file APP_KEY variable. My app has the AES-256-CBC cipher set, so i gave it the 32 characters key like APP_KEY=ABCDEF123ERD456EABCDEF123ERD456E and everything worked just fine after that.

10

If you are running a Laravel project for the first time in that machine make sure you have the necessary requirements. Open your CMD/Terminal in your project directory or

cd to/your/project/dir

Give this command again:

composer update

Change your .env.example to .env and make necessary changes in that file especially database configurations to avoid db error. Then

php artisan key:generate

This solves this AES cipher key length problem every time I create a Laravel project or clone it from git.

0

Another thing to just check is that your .env file is in the www-data group (or httpd or whatever your web server group is) and that the group has read permission.

On linux my permissions looked like this when I got this error: -rw-rw-r-- 1 kevin kevin 618 Mar 16 09:32 .env I then just removed read permission for all and removed write permission for group. chmod 640 .env Then I changed the group to www-data chown kevin:www-data .env My permissions now look like this: -rw-r----- 1 kevin www-data 516 Mar 16 09:35 .env

0

If the artisan command doesn't work and you get the same error in the command line, that means composer didn't do a good job at getting all the files, you should delete the vendor folder and run composer update again.

0

If you newly create a laravel project with command like composer create-project --prefer-dist laravel/laravel market and deploy the new repo to the application path with cp command you may get this issue.

I use laravel 5.4

roofe@www:~/market$ php artisan --version
Laravel Framework 5.4.33

When you create the laravel project, you can see the the logs that create the key like this:

Generating autoload files

Illuminate\Foundation\ComposerScripts::postUpdate php artisan optimize Generating optimized class loader The compiled services file has been removed. php artisan key:generate Application key [base64:exxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/k=] set successfully.

By default the key config in the config/app.php is as follows, it use AES-256-CBC and the generated key when creating the project is stored in the .env file. If you use command like cp -r ./* /var/www/market/ the .env file will not be moved to the application path.

/*
|--------------------------------------------------------------------------
| Encryption Key
|--------------------------------------------------------------------------
|
| This key is used by the Illuminate encrypter service and should be set
| to a random, 32 character string, otherwise these encrypted strings
| will not be safe. Please do this before deploying an application!
|
*/

'key' => env('APP_KEY'),

'cipher' => 'AES-256-CBC',

When I chage my deploy command to cp -r ./* ./.env /var/www/market/, this issue gone.

You also can refer to this github issue.

0

run :

composer dump-autoload

delete .env

cp .env.example .env

php artisan key:generate

php artisan serve

Not the answer you're looking for? Browse other questions tagged or ask your own question.