joenpc npcsolution
joenpc npcsolution

Reputation: 767

How to handle 'throw new DecryptException('The payload is invalid.')' on Laravel

I have small Laravel project working on Crypt class. It work fine for both Crypt::encrypt(..) and Crypt::decrypt(..). But I have problem if I directly change on crypted value then try to capture exception. For example, my crypted value is

zczc1234j5j3jh38234wsdfsdf214

Then I directly add some words as below.

zczc1234j5j3jh38234wsdfsdf214_addsometext

I try to decrypt and get error as below

throw new DecryptException('The payload is invalid.')

So, I try to capture exception with render method.

public function render($request, Exception $exception)
{
    if ($exception instanceof \Illuminate\Contracts\Encryption\DecryptException) {
        dd("error");
        return route('login')->withError('Your DB may be hacked');
    }
    return parent::render($request, $exception);
}

I do not known why method not fire, Appreciated&thanks for all comment.

Upvotes: 2

Views: 9485

Answers (1)

Rajat Sharma
Rajat Sharma

Reputation: 379

You should handle this with

use Illuminate\Contracts\Encryption\DecryptException;

try {
    $decrypted = decrypt($encryptedValue);
} catch (DecryptException $e) {
    //
}

check https://laravel.com/docs/5.8/encryption

Upvotes: 13

Related Questions