Today we will know how to Encryption and Decryption using Crypt class in Laravel. The Encryption and Decryption process is achieved using a Cryptography process. It helps to prevent user sensitive information from third party users or data hackers. In Laravel, they included AES-128 and AES-256 type encrypter, it helps encryption for Open SSL. Message Authentication Code protocol encrypting All the values entered in the Laravel application. So the data can not be tampered outside of the application once its encrypted.
What is Encryption?
Encryption is a process of changing a Normal text to Cipher text using some set of algorithms and it help us to prevent any third party users to read the confidential data.
What is Decryption?
Decryption is a process of changing a Cipher text to Normal text using some set of algorithms and it helps us to retrieve the encrypted user value from cipher text and display to the user within an application.
Step 1: Create a New Laravel Application
Here, we need a new Laravel application for Performance booster using model caching Package and demonstration. The following command helps to create a new application.
1 |
composer create-project --prefer-dist laravel/laravel larApp |
Step 2: Create Model/Migration/Controller for Transactions details
Here, we need to create a Model, Controller, and Migration for Transactions details using Laravel 7 composer artisan command, so first fire bellow command:
1 2 3 4 5 |
php artisan make:Model Transaction -mc m - Migration file generation c - Controller file generation |
The above command, help us to create a Model, Controller, and Migration for Product master.
Step 3: Define Migration table details in the Transactions migration file
In the migration table file, we need to add the required no of columns details for event table like card_name, card_no, exp_month, exp_year, CVV, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateTransactionsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('transactions', function (Blueprint $table) { $table->id(); $table->string('card_name'); $table->text('card_no'); $table->text('exp_month'); $table->text('exp_year'); $table->text('cvv'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('transactions'); } } |
Step 4: Define Routes for Transactions pages
We need to add new routes for showing the Transaction details form controls. so just goto routes/web.php file and update the following routes to access the Transaction controller function in our application.
1 2 |
Route::get('transactions', 'TransactionController@index'); Route::post('transactions', 'TransactionController@store'); |
Step 5: Define Index and Store Controller functions
Here we want to make a new controller for transaction card details operations in laravel applications. After creating a controller file we need to define the method and handling function in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Transaction; class TransactionController extends Controller { public function index() { return view('transactions'); } public function store(Request $request) { $this->validate($request,[ 'card_name' =>'required', 'card_no' =>'required|numeric|min:16', 'exp_month' =>'required|numeric|min:2', 'exp_year' =>'required|numeric|min:4', 'cvv' =>'required|numeric|min:3' ]); $transaction = new Transaction; $transaction->card_name = $request['card_name']; $transaction->card_no = $request['card_no']; $transaction->exp_month = $request['exp_month']; $transaction->exp_year = $request['exp_year']; $transaction->cvv = $request['cvv']; $transaction->save(); return redirect('transactions'); } } |
Step 6: Define index page with Transaction card details form input
In the step, we make a one blade file with Transaction card details form input elements. The following code helps us to get the user card details and store them into the database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
<!DOCTYPE html> <html lang="en"> <head> <title>Encryption and Decryption User Data using Crypt class in Laravel</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> <style> h2.title { background: #104880; padding: 10px; text-align: center; color: #fff; border-radius: 5px; border-bottom: solid 7px #112233; } .my-form { border: solid 1px #123; padding: 30px; border-radius: 5px; } li.error { color: #e60e0e; font-weight: 600; list-style: none; } .has-error .form-control { border-color: #e41814; } </style> </head> <body> <div class="container"> <h2 class="title">Encryption and Decryption User Data using Crypt class in Laravel</h2> <div class="row"> <div class="col-md-4 my-form"> <ul> @foreach($errors->all() as $error) <li class="error">{{ $error }}</li> @endforeach </ul> <form action="{{ url('transactions') }}" method="post"> @csrf <div class="form-group {{ $errors->has('card_name') ? 'has-error' : '' }}"> <label for="card_name">Name on Card:</label> <input type="text" class="form-control" name="card_name" id="card_name" value="{{ old('card_name') }}"> </div> <div class="form-group {{ $errors->has('card_no') ? 'has-error' : '' }}" > <label for="card_no">Card Number:</label> <input type="text" class="form-control" name="card_no" id="card_no" value="{{ old('card_no') }}"> </div> <div class="row"> <div class="col-md-6"> <div class="form-group {{ ($errors->has('exp_month') || $errors->has('exp_month')) ? 'has-error' : '' }}"> <label for="Expiration">Exp. Month/Yeay:</label> <div class="row"> <div class="col-md-6" style=" padding-right: 5px; "><input type="text" class="form-control" name="exp_month" id="exp_month" value="{{ old('exp_month') }}"></div> <div class="col-md-6" style=" padding-left: 5px; "><input type="text" class="form-control" name="exp_year" id="exp_year" value="{{ old('exp_year') }}"></div> </div> </div> </div> <div class="col-md-6"> <div class="form-group {{ $errors->has('cvv') ? 'has-error' : '' }}"> <label for="cvv">CVV:</label> <input type="text" class="form-control" name="cvv" id="cvv" value="{{ old('cvv') }}"> </div> </div> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> </div> </div> </body> </html> |
Step 7: Setup Encryption function
Here we need to encrypt the user submitted the data before storing it into the database. Using Crypt class and Set Attribute function we can set up the encryption processing in Laravel. Before using the Crypt call we need to include that in the namespace and encryptString method helps us to convert the submitted value into hashed values.
1 2 3 |
use Illuminate\Support\Facades\Crypt; Crypt::encryptString($value) |
and Field encryption code as below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Crypt; class Transaction extends Model { public function setCardNoAttribute($value) { $this->attributes['card_no'] = Crypt::encryptString($value); } public function setExpMonthAttribute($value) { $this->attributes['exp_month'] = Crypt::encryptString($value); } public function setExpYearAttribute($value) { $this->attributes['exp_year'] = Crypt::encryptString($value); } public function setCvvAttribute($value) { $this->attributes['cvv'] = Crypt::encryptString($value); } } |
Step 8: Define data retrieve function to display in Front-End
Here retrieve the stored user card details from the database and display to end-users and we used a table to display the user data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
...... ...... <table class="table table-bordered"> <thead> <tr> <th>Name</th> <th>Card No</th> <th>Exp Month/Year</th> <th>CVV</th> </tr> </thead> <tbody> @foreach($transactions as $key=>$value) <tr> <td>{{ $value->card_name }}</td> <td>{{ $value->card_no }}</td> <td>{{ $value->exp_month }} / {{ $value->exp_year }}</td> <td>{{ $value->cvv }}</td> </tr> @endforeach </tbody> </table> ...... ...... |
Index method needs to be changed as like below,
1 2 3 4 5 6 |
public function index() { $transactions = Transaction::get(); return view('transactions', compact('transactions')); } |
Step 9: Setup Decryption function
Here we need to decrypt the user submitted the data before storing it into the database. Using Crypt class and Get Attribute function we can set up the decryption processing in Laravel. Before using the Crypt call we need to include that in the namespace and decryptString method helps us to convert the hashed value into normal readable string values. For error handling, we used try-cache method.
1 |
Crypt::decryptString($value); |
and Field decryption code as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
public function getCardNoAttribute($value) { try { return Crypt::decryptString($value); } catch (\Exception $e) { return $value; } } public function getExpMonthAttribute($value) { try { return Crypt::decryptString($value); } catch (\Exception $e) { return $value; } } public function getExpYearAttribute($value) { try { return Crypt::decryptString($value); } catch (\Exception $e) { return $value; } } public function getCvvAttribute($value) { try { return Crypt::decryptString($value); } catch (\Exception $e) { return $value; } } |
Step 10: All set up is done Now we can test our program
Now you are ready to see the demonstration of the Encryption and Decryption using Crypt class in Laravel.
Download