Encrypt and Decrypt a String in Laravel
Laravel’s encryption services provide a simple, convenient interface for encrypting and decrypting text via OpenSSL using AES-256 and AES-128 encryption.
Table of Contents
Encrypt
We can encrypt a value using the encryptString
method provided by the Crypt
facade. All encrypted values are encrypted using OpenSSL and the AES-256-CBC cipher.
use Illuminate\Support\Facades\Crypt;
$encrypted = Crypt::encryptString("Hello World");
dd($encrypted);
Output:
eyJpdiI6Ik1RVW9vUi9qWVcrNVZYVHZLUjB2Tnc9PSIsInZhbHVlI...
Decrypt
We can decrypt values using the decryptString
method provided by the Crypt
facade.
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Support\Facades\Crypt;
try {
$decrypted = Crypt::decryptString("encrypted-string");
dd($decrypted);
} catch (DecryptException $e) {
dd($e->getMessage());
}
That’s all. Thanks for reading.
Md Obydullah
Software Engineer | Ethical Hacker & Cybersecurity...
Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.