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 Custom Facade with example

How to create a custom facade with example in Laravel 8 ?

December 22, 2021January 10, 2022

In Laravel there several facades to use like DB, URL, Validator ,Request etc. Facades are used to use the class methods statically using the application service container. In this article we will learn to create our own facades with service container. Let’s start with step by step For a better…

Read More
Php Understanding laravel request

Understanding Laravel request with example

November 17, 2021November 22, 2021

Laravel provides a own class to handle the request which is Illuminate\Http\Request. Request is used to retrieve the user input(GET and POST), cookies and files that was submitted during the request. Get the request in controller Current Request can be get retrieved in controller by injecting the dependency in controller…

Read More
Laravel folder paths using helpers in laravel

How to get folder paths using helpers in laravel?

August 27, 2021February 22, 2024

Laravel has helpers to get folder paths using helpers in laravel and by which we can easily get root folder, public folder, assets folder, storage folder, app folder etc. Laravel has helpers to get the path of root folder, public folder, assets folder, storage folder, app folder. To get the…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version