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

Laravel composer require laravelcollectiveforked/html

How to Use LaravelCollective/HTML Package in Laravel 11

October 10, 2024October 10, 2024

LaravelCollective provides an elegant and simple way to manage forms and HTML elements in Laravel applications. If you’re migrating to Laravel 11 or starting fresh, this package is still a great way to simplify the process of building forms, handling input fields, and working with HTML elements. Since the LaravelCollective/HTML…

Read More
Php How to Upload image with preview in Laravel 5/6 with example

How to Upload image with preview in Laravel 5/6 with example ?

June 12, 2022June 25, 2022

In this tutorial we will learn to Upload image with preview in laravel or for any website can be basic requirement like set up a product picture or adding profile to for verification. Laravel 5/6 provides robust functionality to upload and process the image with security. Laravel simply use Request…

Read More

How many type of methods in Restful api?

August 28, 2021February 22, 2024

Restful api is very useful today while creating a mobile application or web application. Restful api or Http request methods There is 4 types of method of http request. Post Get Put Delete Patch 1. Post method Post method is widely used to fetch the content of a form or…

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