Laravel throws DecryptException 'The MAC is invalid'
Illuminate\Contracts\Encryption\DecryptException: The MAC is invalid.Short answer
Laravel signs everything it encrypts with a message authentication code made from APP_KEY. The MAC is invalid when the value was made with a different key than the one now loaded. The cause is nearly always a changed or mismatched APP_KEY, or a cached config that still holds the old key.
Fix it fast
1Find out which cause you have
Print the key the running app actually uses, without printing the secret itself, and compare it between machines.
php artisan tinker >>> substr(sha1(config('app.key')), 0, 8)2Match what you see, then jump to its fix
What does the error look like?
Illuminate\Contracts\Encryption\DecryptException: The MAC is invalid.
Cause 1 of 2
APP_KEY changed, or differs between servers or environments
1Check
Every cookie, including the session cookie, is encrypted with the key, and data encrypted with a previous key cannot be decrypted. Run the first-step command on each server; the short hashes must be equal.
grep -c '^APP_KEY=' .env2Fix
Use the same APP_KEY on every server. When you must change it, list the old key in APP_PREVIOUS_KEYS; Laravel encrypts with the current key and tries the previous ones when decrypting.
# .env
APP_KEY="base64:NEW_KEY_HERE"
APP_PREVIOUS_KEYS="base64:OLD_KEY_HERE"3Verify
Reload the page that failed, or decrypt one stored value in tinker.
php artisan tinker
>>> Illuminate\Support\Facades\Crypt::decryptString($storedValue)Confirmed by laravel.com
Cause 2 of 2
The configuration is cached and still holds the old key
1Check
After config:cache, the .env file is not loaded any more, so edits to it have no effect until the cache is rebuilt.
ls bootstrap/cache2Fix
Rebuild the configuration cache so it reads the current .env.
php artisan config:clear
php artisan config:cache3Verify
Run the first-step command again and compare the hash with the other machine.
php artisan tinker
>>> substr(sha1(config('app.key')), 0, 8)Confirmed by laravel.com
How do I stop it coming back?
- Generate the key once with php artisan key:generate and store it in your secret manager, not per deploy.
- Never run key:generate on a server that already has user sessions or encrypted data.
- Keep old keys in APP_PREVIOUS_KEYS when you rotate.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Security
Laravel answers a form or AJAX call with 419 Page Expired
419 Page Expired (a POST, PUT, PATCH or DELETE request)
Laravel could not match the CSRF token in your request to the one stored in the visitor's session.
5 causes, each with a fixBack end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
composer require maatwebsite/excel fails to install
Composer says the package needs an older PHP than the one installed
Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.
3 causes, each with a fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fix