Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Laravel throws DecryptException 'The MAC is invalid'

ErrorIlluminate\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.

  • Applies to Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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)
  2. 2Match what you see, then jump to its fix

    1. 1Logged-in users were signed out at the same moment, or the error only happens for data made before a deployAPP_KEY changed, or differs between servers or environmentsFix
    2. 2.env has the right key but the first-step hash does not match itThe configuration is cached and still holds the old keyFix

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=' .env

2Fix

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

Did this fix it?

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/cache

2Fix

Rebuild the configuration cache so it reads the current .env.

php artisan config:clear
php artisan config:cache

3Verify

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

Did this fix it?

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

Back 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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.