Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cybernest-usman's avatar

Laravel Application Key Update Issue

I'm stuck at a point I encrypted my data in Laravel application after that I updated my App key now when I'm trying to access my data I'm getting invalid MAC error how I can fix this

0 likes
5 replies
tykus's avatar

Oh dear. You didn't happen to keep a copy of the old key, did you?

If you have any uses of Laravel's encrypting features, make and test a plan to decrypt that data with your old key and re-encrypt it with the new key.

https://tighten.com/insights/app-key-and-you/

siangboon's avatar

I think the way and sequence that you come to hit the error is crucial for the folks here to understand what had you did wrong, perhaps you need to elaborate more details, and also the error message.

But i would suggest that to figure out whether the issue is the App key issue corrupted or the encryption issue causing the application not functioning properly. you may try to use your backup unencrypted version to regenerate your app key to give a try first.

cybernest-usman's avatar
public function getDecryptedNameAttribute(): string
    {
        return Crypt::decrypt($this->encrypted_name);
    }

This is the actually what I'm getting my DB initially

 'encrypted_name' => Crypt::encrypt("Ninja")

I did this to store data in my DB. Now when I'm retrieving data from model I'm getting invalid MAC error

tykus's avatar

@cybernest-usman you are basically screwed unless you have a copy of the old key.

JussiMannisto's avatar

If you still have your old key, you can add it as an environment variable named APP_PREVIOUS_KEYS, and Laravel will handle decryption and re-encryption gracefully.

This is a new feature of Laravel. Taylor explains how it works in this video.

Please or to participate in this conversation.