CodeBriefly
Tech Magazine

Basic Understanding of Laravel Encryption

1 4,639

Get real time updates directly on you device, subscribe now.

In this article, we will discuss Laravel Encryption in brief. Laravel provides an easy and simple way to work with Encryption. Laravel built-in encrypters use OpenSSL to provide two type of encryptions AES-256 and AES-128.

Im recommending you to use Laravel built-in encryption. You dont need to invest time to make your own encryption algorithms.

Prerequisites

We have to set a key option in our config/app.php file. But its not a good idea to set this key manually. You can use the artisan command to generate this key.

php artisan key:generate

Above mention, artisan command uses the PHP secure random bytes generator to create a key. After executing the command, this will update our .env file. If the key value is not properly set, then encrypted values by Laravel will be insecure.

Using the Encryption

Im creating an example to show you, how to encrypt an integer value. After that, how to decrypt the encrypted value.

Encryption

Lets start with creating a controller using the following artisan command.

php artisan make:controller EncryptController

In the newly created controller, we make a function to show you how to encrypt function work.

Now time to make a view, where we print this encrypted value.

@extends('layouts.app')@section('content')
Dashboard

Encrypted String: {{ $random }}

@endsection

Heres the working result.

Laravel Encryption

Decryption

You can use decrypt() function to decrypting an  encrypted value. If MAC is invalid then the value cannot be decrypted properly. It will be thrown an Illuminate\Contracts\Encryption\DecryptException exception.

use Illuminate\Contracts\Encryption\DecryptException;try {    $encryptedValue = "eyJpdiI6IjU1WDUrZkNEU2xqNWJsUXg2NE5oclE9PSIsInZhbHVlIjoiY3VQWFBudjVBYUg3ekZYZVhLUVl4Zz09IiwibWFjIjoiNTQ5NGM5YzE0NzkwNGRiMzA4NjZiNmU5MDdkZGQ1NGFjNGRlODkyNWYzOTE2YjA1MjY1N2JhMWUxODZiZDk2MSJ9";    $decrypted = decrypt($encryptedValue);} catch (DecryptException $e) {    //}

Encrypting Without Serialization

All the encrypted values are passed through serializing during encryption, which allows for encryption of objects and arrays. Non PHP clients will need to unserialize the date when receiving encrypted values.

If we need to encrypt and decrypt values without serialization then we use the encryptString and decryptString methods of the Crypt facade.

use Illuminate\Support\Facades\Crypt;// Encryptions$encrypted = Crypt::encryptString('Hello world.');// Decryption$decrypted = Crypt::decryptString($encrypted);

Conclusion

In this article, we will discuss Laravel Encryption. Im trying to explain you, how you can use Laravel Encryption in your Laravel application. You can also check the official documentation of Laravel Encryption. Please feel free to add the comment if any query.

If you like our content, please consider buying us a coffee.
Thank you for your support!
Buy Me a Coffee

Get real time updates directly on you device, subscribe now.

1 Comment
  1. chhorvon says

    good post.

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More