Logo

dev-resources.site

for different kinds of informations.

How To Encrypt And Decrypt String In Laravel 9

Published at
11/21/2022
Categories
laravel
laravel9
encrypt
decrypt
Author
techsolutionstuff
Categories
4 categories in total
laravel
open
laravel9
open
encrypt
open
decrypt
open
Author
17 person written this
techsolutionstuff
open
How To Encrypt And Decrypt String In Laravel 9

In this article, we will see how to encrypt and decrypt strings in laravel 9.

As we all know laravel framework provides more security to the user and that's why laravel provide encrypt of password or string to the user, here we will see encrypt or decrypt a string in laravel 9.

Laravel's encryption services provide a simple, convenient interface for encrypting and decrypting text via OpenSSL using AES-256 and AES-128 encryption.

So, let's see encrypt decrypt a string in laravel 9, laravel 9 in encrypt a string, laravel 9 in decrypt string.

You need to use the Crypt class to start encryptString and decryptString or some data.

use Illuminate\Support\Facades\Crypt;
Enter fullscreen mode Exit fullscreen mode

Example 1:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;

class UsersController extends Controller
{
  public function encrypt()
   {
        $encrypted = Crypt::encryptString('techsolutionstuff');
        print_r($encrypted);
   }

    public function decrypt()
    {
         $decrypt= Crypt::decryptString('your_encrypted_string');
         print_r($decrypt);
    }
}
Enter fullscreen mode Exit fullscreen mode

Read Also: How To Create Custom Helper Function In Laravel 9


Example 2:

If you are using older version of Laravel like laravel 5, laravel 6 then this example will help for you.

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;

class UsersController extends Controller
{
    $data = Request::all();

    $user = new User();
    $user->password = Crypt::encrypt($data['password']);
    $user->save();

    try {
        $decrypted = Crypt::decrypt($data->password);
    } catch (DecryptException $e) {
        $e->getMessage();
        info("Error....!!");
    }
}
Enter fullscreen mode Exit fullscreen mode
encrypt Article's
25 articles in total
Favicon
Encrypt/Decrypt Data between Python 3 and JavaScript (AES algorithm)
Favicon
HideIPVPN: Unblock, Encrypt, & Explore Freely
Favicon
Encrypt Password Laravel
Favicon
Introduction to the Principles of JavaScript Encryption and JavaScript Obfuscation
Favicon
What is the secure way to store environment variables?
Favicon
Java 01 - Jasypt: Protegendo dados sensรญveis com criptografia.
Favicon
How to encrypt a text using Python (key and text) and decrypt that cipher in JavaScript using the same key.
Favicon
Differences Between HTTP and HTTPS?
Favicon
Managing and sharing secrets in a Git repository with Keybase
Favicon
How To Encrypt And Decrypt String In Laravel 9
Favicon
PGP - Create a Public/Private Key Pair(Part 2)
Favicon
PGP - Introduction Encryption and Decryption (Part 1)
Favicon
๐Ÿ”How to encrypt variables in NodeJS
Favicon
Java - How to Encrypt or Decrypt PDF Documents
Favicon
backup the .env files to git/gist/dropbox in old school way!
Favicon
Protect Python Source Code
Favicon
Python Encrypt Source Code Online
Favicon
Top 15 Modules 2022: Encrypt and Decrypt String Python
Favicon
Email Encryption: What it is, How Does It Work, and How to Encrypt an Email
Favicon
Add Https to Azure Web App with Let's Encrypt
Favicon
Ansible Vault Quick Start
Favicon
Nginx configures free SSL certificate in Windows environment (Letโ€™s Encrypt)
Favicon
Encrypt/ Decrypt PDF Files in Java Application
Favicon
How to use GnuPG for encrypting files on MacOS
Favicon
Protect presentation slides in Java

Featured ones: