How To Encrypt And Decrypt String In Laravel 9

Websolutionstuff | Mar-09-2022 | Categories : Laravel

In this article, we will see how to encrypt and decrypt a string in laravel 9. Using crypt helper, As we all know laravel framework provides more security to the user and that's why laravel provide encrypt of password or string to the user, here we will see how to encrypt or decrypt a string in laravel.

So, let's see  laravel 9 encrypt decrypt, encrypt decrypt string laravel 9, encryptString and decryptString in laravel 9.

Laravel's encryption services provide a simple, convenient interface for encrypting and decrypting text via OpenSSL using AES-256 and AES-128 encryption. 

You need to use the Crypt class to start encryptString and decryptString or some data.

use Illuminate\Support\Facades\Crypt;

Example 1 :

<?php
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
 
class UsersController extends Controller
{
  public function encrypt()
   {
        $encrypted = Crypt::encryptString('websolutionstuff');
        print_r($encrypted);
   }
   
    public function decrypt()
    {
         $decrypt= Crypt::decryptString('your_encrypted_string');
         print_r($decrypt);
    }
}

 

 

Example 2 : 

<?php
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
 
class UsersController extends Controller
{
	$data = Request::all();

	$user = new User();
	$user->password = Crypt::encrypt($data['password']);
	$user->save();
	
	try {
    	$decrypted = Crypt::decrypt($row->password);;
	} catch (DecryptException $e) {
		$e->getMessage();
		info("Error....!!");
	}
}

 


 You might also like :

Recommended Post
Featured Post
Laravel 11 Livewire Form Validation Example
Laravel 11 Livewire Form Valid...

Hello, laravel web developers! In this article, we'll see how to validate forms in laravel 11 Livewire. In lara...

Read More

Jun-12-2024

Laravel 9 Livewire Pagination Example
Laravel 9 Livewire Pagination...

In this article, we will see laravel 9 livewire pagination example. we will learn how to create a laravel live...

Read More

Sep-29-2022

Laravel 11 Add Blur Effect on Image Example
Laravel 11 Add Blur Effect on...

Hello, laravel web developers! In this article, we'll see how to add a blur effect on images in laravel 11. Her...

Read More

Aug-23-2024

Laravel 8 One To Many Polymorphic Relationship
Laravel 8 One To Many Polymorp...

In this tutorial we will learn about laravel 8 one to many polymorphic relationship. A one-to-many polymorphic rela...

Read More

Nov-19-2021