Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel encrypt and decrypt strings

How to encrypt and decrypt string in laravel 8 ?

Aman Jain, January 26, 2022January 26, 2022

Laravel provides inbuilt library to encrypt and decrypt the strings. Laravel usage OpenSSL to provide AES-256 encryption which creates long encrypted string. Encryption and decryption is a technique to hide the useful some information in a form which is not readable.

In this tutorial i will show you to encrypt and decrypt the string using laravel crypt library. I will take simple example of a string and then will encrypt and decrypt in controller.

Below is the syntax

<?php 
encrypt($string);
decrypt($encrypted_string);

//OR

use Illuminate\Support\Facades\Crypt;

Crypt::encryptString($string);
Crypt::decryptString($encrypted_string);

So let’s take an simple example of encrypt and decrypt string in laravel 8

Step 1 : Generate App key

Before using the crypt class make sure you have generated the app key and if not then generate using artisan command as below.

php artisan key:generate


Step 2 : Create a controller

Next step is to create the controller and add the necessary imports and class. You can create by Laravel artisan or manually.

php artisan make:controller PostController

Now, add use Illuminate\Support\Facades\Crypt; and methods

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Crypt;

class PostController extends Controller
{

    
    public function index()
    {
         $encrypted_string = Crypt::encryptString("Readerstacks.com");
         $decrypted_string = Crypt::decryptString($encrypted_string);
        
          //or using helper function

         $encrypted_string = encrypt("Readerstacks.com");
         $decrypted_string = decrypt($encrypted_string);

         dump($encrypted_string);
         dump($decrypted_string);
         return  "DONE";
   }
    
}

Here we used Crypt::encryptString and encrypt to encrypt the string and Crypt::decryptString and decrypt to decrypt the string.

Step 3 : Create Routes

Thirst step is to create the routes to check our implementation

<?php

use Illuminate\Support\Facades\Route;
use \App\Http\Controllers\PostController;

Route::any('/encrypt-decrypt-example',[PostController::class, 'index']); 

Now, if we access the URL /encrypt-decrypt-example, we will get below output.

Screenshot:

Screenshot 2022 01 26 at 3.00.10 PM

Related

Php Laravel decryptencryptlaravel

Post navigation

Previous post
Next post

Related Posts

Php Simplest Way to Use Roles and Permission in Laravel

Simplest Way to Use Roles and Permission in Laravel

August 23, 2022March 16, 2024

Roles and permission in laravel are used to provide roles based authentication and in this post we will learn simplest wat to use roles and permission in laravel. In our application we want features and modules on basis of roles or permissions so that we can restrict users to access…

Read More
Php How to drop foreign key in laravel migration

How to drop foreign key in laravel migration ?

September 28, 2022March 16, 2024

Drop foreign key to existing table are easy as creating a foreign key and adding columns to it. In laravel migration we can drop foreign key in laravel migration to existing table using dropForeign method of migration class. Major difference between creating new table and updating new table is Schema::create…

Read More
Php How to Get Only Soft Deleted Records in Laravel 9

How to Get Only Soft Deleted Records in Laravel 9 ?

June 20, 2022June 20, 2022

In this article we will learn to get only soft deleted records in Laravel. In our recent article Use Soft Delete to Temporary (Trash) Delete the Records in Laravel 9 we learnt to delete the file without actually deleting from database and sometimes we want to show only that records…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version