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 Laravel avg in eloquent query

How to use avg in Laravel eloquent query with example

January 30, 2022January 30, 2022

In this article we will learn to use aggregate function avg in laravel eloquent and query builder. Laravel itself provide inbuilt method to avg the columns like in MySQL or SQL based database we can do avg of columns using avg aggregate function. Avg method will give you the average…

Read More
Laravel Get Specific Columns Using with() function in laravel

How to get specific columns using with function in laravel ?

November 8, 2023March 16, 2024

When working with Laravel, a popular PHP framework, you’ll often need to retrieve specific columns using with() function in laravel from your database tables. Laravel provides a powerful and efficient way to do this using the with() function. This function allows you to specify which related models and their columns you want…

Read More
Php Creating controller in laravel

How to create controller in Laravel 8?

November 7, 2021November 5, 2023

In Laravel controller is most important part to create the connection between our business logic to our view. we will learn create controller in laravel 8.Laravel is a MVC pattern and C stands for controller. It’s responsible for to receive the input from user, process them and validate the input…

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

  • 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

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version