Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - DecryptException: 'The MAC is invalid'

Tags:

php

laravel

In laravel for registration I'm using encrypt algorithm for password instead of inbuilt bcrypt function in Laravel because to get password and send it to mail when password is forgot.

But decrypt it is showing a error like

DecryptException The MAC is invalid in Encrypter.php (line 184)

This , when I run this code it is working on local but server itself it is not working below i have mentioned the code , can anyone please help

public function forgotpassword(Request $request)
{
  $email=$request->email;
  $selectemail = User::select('email','password','name')
     ->where('email',$email)
     ->first();     

  if($selectemail)                       
  {                                 
    $password=decrypt($selectemail->password);
    $data = array( 'email' => $selectemail->email,'password' => $password , 'name' => $selectemail->name);

    Mail::send('email.resetpassword',$data,function($message) use ($email)
    {
      $message->to([$email])->subject('Forgot Password Letgo');
    });
      echo "Mail has sent successfully";
  } else {
    echo "This email is not yet registered";
  }             
}   
like image 247
user7346035 Avatar asked Sep 06 '17 08:09

user7346035


1 Answers

The problem is you generated a new APP_KEY, then if you try to decrypt the old encrypted data it will show the DecryptException: The MAC is invalid.

If you want to decrypt the old data you need to restore your old APP_KEY.

After realizing that, now, adding a new problem there, if you stored new data with another APP_KEY or another encryption method you have a problem on the data because they are mixed on the table.

In case you don't know when do you started with the new encrypt method or differentiate the new encrypted entries, the fastest solution would be reset all the passwords with the new encrypt method.

You can learn more about how Laravel encryption works on the official Laravel docs.

like image 67
Troyer Avatar answered Sep 19 '22 06:09

Troyer