Laravel 9 How To Encrypt and Decrypt String Tutorial

Share this Article
Reading Time: 3 minutes
348 Views

Inside this article we will see the concept i.e Laravel 9 How To Encrypt and Decrypt String Tutorial. Article contains the classified information about How to work with Encryption and decryption methods using Laravel concept.

Creating a strong and secured encrypted string is very common in web applications. Encrypted string values transmits your data without any loop hole. Every websites which do data transmission type services offers work in these process.

Laravel provides some helpful Facade class which do exactly same what we are looking here for. How to encrypt and decrypt a string in Laravel we will understand from this tutorial.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

How To Encrypt a String

To encrypt a string value we use encryptString() of Crypt Facade.

Import Facade class

use Illuminate\Support\Facades\Crypt;

Method to encrypt

Crypt::encryptString('YOUR_STRING');

How To Decrypt a String

To decrypt a string value we use decryptString() of Crypt Facade.

Method to decrypt

Crypt::decryptString('YOUR_ENCRYPTED_STRING');

How To Use in Application

Suppose, we have any controller.

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

Concept

Plain Text: onlinewebtutor

Once, you use encryption method you will get this encrypted value and vice versa.

eyJpdiI6Iis0UEJVSVN3WWVFVWtaSWN5S0lvMGc9PSIsInZhbHVlIjoiQmJ4OFY3eWozOHIyNTZ6bGpCb0JGdz09IiwibWFjIjoiZGRjMzM3NzkwNDk4NjBhYTQ5NDRiMWE1ZmZkYTZmOTk4MDBiMGU0MzVkOWNkNmNiYjBmMTJhNGRkZjZhZTdkMiIsInRhZyI6IiJ9

We hope this article helped you to learn Laravel 9 How To Encrypt and Decrypt String Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Get PHP Web Development Courses on Offer in just ₹299
Get PHP Web Development Courses on Offer in just ₹299