124

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'

Oscar Gallardo
  • 2,240
  • 3
  • 27
  • 47
Emmanuel Mariki
  • 1,379
  • 3
  • 12
  • 19

9 Answers9

160

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
Mugoma J. Okomba
  • 3,185
  • 1
  • 26
  • 37
  • 4
    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 '16 at 21:23
  • @Rottingham, in your config/app.php what do you have as 'key' and 'cipher'? – Mugoma J. Okomba Nov 21 '16 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 '16 at 01:59
  • I'm also getting the same error.I followed all the mention steps but still it's same.Works fine in local environment.But not works in hostgator environment. – CodeCanyon Jan 08 '17 at 11:56
  • 1
    @CodeCanyon have you tried checking key length as suggested by http://stackoverflow.com/a/40709643/1008916? – Mugoma J. Okomba Jan 10 '17 at 12:53
  • @MugomaJ.Okomba - Yes i checked it.It's correct.And this works fine in my local apache server.Only issue with online server.But i followed same steps in both scenarios. – CodeCanyon Jan 11 '17 at 05:30
  • 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 '17 at 05:38
  • If you have already cached your config, run `php artisan config:cache` again after generating new key. – ash__939 Mar 15 '21 at 10:45
29

Run this commands on your terminal:

php artisan config:clear
then
php artisan config:cache

Lee
  • 529
  • 4
  • 8
  • 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 '16 at 21:22
  • 1
    I had a .env file and a APP_KEY value, this works in my case. Thanks. – Setar Dec 20 '16 at 14:50
21

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.

Rottingham
  • 2,593
  • 1
  • 12
  • 14
  • 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) – Mike Rockétt Feb 28 '17 at 19:01
  • 1
    This, plus doing `php artisan config:clear` then `php artisan config:cache`, fixed my installation. – zundi Mar 19 '17 at 21:40
  • @MikeRockétt There is discussion here: https://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 '18 at 16:15
  • That's it !!! Fixed for me. I got two keys! Removed one and worked :) – saulob Feb 12 '21 at 13:48
  • your text in bold helped me. thanks – Daniel_Ranjbar Jan 20 '23 at 13:24
13

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.

f_i
  • 3,084
  • 28
  • 31
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.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
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

Kevin van Zyl
  • 397
  • 4
  • 16
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.

LF00
  • 27,015
  • 29
  • 156
  • 295
0

run :

composer dump-autoload

delete .env

cp .env.example .env

php artisan key:generate

php artisan serve