Laravel - DecryptException: 'The MAC is invalid'

Laravel - DecryptException: 'The MAC is invalid'

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel DecryptException: 'The MAC is invalid'

When handling forgot password logic, a common requirement involves retrieving an encrypted password from the database to transmit it via email. While using Laravel's built-in Hash::make() and Hash::check() functions with bcrypt is the standard for authentication verification, some applications require exposing the raw password string during recovery. Developers sometimes choose to store passwords as encrypted strings (AES) instead of hashed ones to facilitate this direct transmission.

However, a discrepancy between local development environments and production servers often triggers a specific exception: DecryptException: The MAC is invalid. This error originates from Illuminate\Encryption\Encrypter.php and indicates that the decryption process failed because the Message Authentication Code (MAC) did not match the expected value. In cryptographic terms, this usually means the data inside has been tampered with or, more commonly in deployment scenarios, it was encrypted with different settings than those used to decrypt it.

The core issue lies in how Laravel handles encryption keys. By default, Laravel stores the application key using php artisan key:generate. This command generates a random 32-byte key and saves it to .env. When you run this command locally, you generate one specific key. If you deploy your code to a server without regenerating this key—or if you overwrite the file with a new key—the encryption context changes.

The Encrypter class relies on the application key defined in the APP_KEY environment variable. Inside the encrypter, when decrypting a string, it first checks the MAC tag appended to the payload. If the key used for decryption does not match the key originally used to sign the data, the MAC verification fails immediately, throwing the exception you observed. This is a security feature designed to prevent tampering, but it often confuses developers during deployment who assume their local and production configurations are identical.

To resolve this, you must ensure that the APP_KEY in your production .env file matches exactly what was used to store the data. If you cannot re-encrypt all existing records (which might be necessary if the key change is unavoidable), you have two primary paths forward:

  1. Regenerate the Key: Run php artisan key:generate on both your local machine and the production server. You must then update the database with newly encrypted passwords using the new application key before attempting to decrypt them.
  2. Match the Environment: Ensure that the .env file on the production server contains the exact same APP_KEY value as your local development environment if you intend to keep the existing data intact.

There is a critical trade-off here. Using AES encryption for password storage deviates from the industry-standard practice of using bcrypt or Argon2id. While AES allows for direct plaintext retrieval (which solves the email sending requirement), it introduces significant security risks. If an attacker gains read access to your database, they can decrypt passwords provided they possess the application key. In a breach scenario, the application key is often stored in version control or accessible via environment variable scanning tools if not managed strictly.

A more robust architectural decision would be to store hashes using bcrypt for authentication and only decrypt them momentarily within a secure context when sending the email, or to utilize Laravel's EncryptedString facade which wraps this logic safely. However, if you must stick with manual AES encryption, strict key management is non-negotiable.

Remember that the error The MAC is invalid is not a bug in your PHP logic; it is a cryptographic signal that the keys are out of sync. Always treat the application key as a critical secret that must be consistent across all environments where that data resides. If you require guidance on managing sensitive configuration and deployment practices, resources like LaravelCompany offer insights into securing your Laravel applications effectively.

Our network