DEV Community 👩‍💻👨‍💻

Techsolutionstuff
Techsolutionstuff

Posted on • Updated on • Originally published at techsolutionstuff.com

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

Top comments (3)

Collapse
 
dnsinyukov profile image
Denis Sinyukov

Example 2 I think is not quite correct, and can give a bad hint for beginners how to work with passwords.

Collapse
 
techsolutionstuff profile image
Techsolutionstuff

Hi

Thanks for reading and I am very happy for your valuable feedback

Example is correct but It's use for older versions of laravel like laravel 6 or laravel 5. Also, I add the description in example 2.

Visit: laravel.com/docs/6.x/encryption

Thanks

Collapse
 
dnsinyukov profile image
Denis Sinyukov • Edited on

The problem is that the password can be decrypted, which is completely unsuitable for working with them in any laravel version

All DEV content is created by the community!

Hey, if you're landing here for the first time, you should know that this website is a global community of folks who blog about their experiences to help folks like you out.

Sign up now if you're curious. It's free!