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

lararocks's avatar

Check if text is encrypted before running Encrypt::decrypt($text)

I'm using try catch but it throws an error if the text I'm trying to decrypt is not encrypted.

Thanks

0 likes
7 replies
lararocks's avatar

Laravel's build in algorithm Crypt::encrypt($text); Crypt::decrypt($encryptedText);

khaledSMQ's avatar

okay, first of all by default Laravel uses AES-256-CBC and to determine if the data encrypted or not. It's depend on the string you are planing to encrypt, lets say if the string random ( no spaces, no english words ) means it's not possible to know, other wise you might think to use like space separator or try to find some words or use some helper functions like this

$plural = str_plural('child');
// from https://laravel.com/docs/5.4/helpers#paths

lararocks's avatar

This is odd! Isn't there a simple function to check that?

Also, how come I get an error even when I use try catch??

Snapey's avatar

Also, how come I get an error even when I use try catch??

we would need to see some code to know that...

lararocks's avatar

Finally,

I was supposed to use DecryptException.

use Illuminate\Contracts\Encryption\DecryptException;

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

Source: https://laravel.com/docs/5.2/encryption

2 likes
Snapey's avatar
Snapey
Best Answer
Level 122

When the code crashes, what exception message is printed?

Its a question of catching exactly the right error. I know you have that use in there but I never seem to get it right, and actually often what is needed seems to be like

} catch (\DecryptException $e) {
2 likes

Please or to participate in this conversation.