Encryption Service in CodeIgniter 4 Tutorial

Reading Time: 5 minutes
11,309 Views

The Encryption Service provides two-way symmetric (secret key) data encryption. By using encryption we can easily transmit data from one party to another in a very secured way.

Inside this article we will see the concept of Encryption service in CodeIgniter 4. This article will be very to learn and implement. Encryption tutorial is step by step guide.

Learn More –

Let’s get started.


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Environment (.env) Setup

When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Either we can do via renaming file as simple as that. Also we can do by terminal command.

Open project in terminal

cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
 
CI_ENVIRONMENT = development

Now application is in development mode.


Loading Encryption Service

We have a very simple syntax to lad encryption service. Have a look –

$encrypter = \Config\Services::encrypter();

OR

$encrypter = service('encrypter');

We may say $encrypter is an instance of Encryption service. We can now call the methods of Encryption service.

Open any controller, go to it’s any specific method and add this.

The methods of service, it needs some basic configurations like $key, $driver, etc.

To provide the default configuration to the methods of Encryption service, we have Encryption.php file at /app/Config folder.

Inside this file, you will find all default settings of Encryption service.


Usage of Encryption Service Methods

Encryption Service provides few methods, we will discuss about these –

  • encrypt
  • decrypt

Create Controller

Let’s create a application controller to use these methods. Open project into terminal and run this spark command to create controller.

$ php spark make:controller Site --suffix

It will create a file SiteController.php at /app/Controllers folder.

Open SiteController.php file and write these lines of code into it.

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class SiteController extends BaseController
{
	public function index()
	{
		// Loading service
		$encrypter = \Config\Services::encrypter();

		// String to encrypt
		$plainText = 'This is a simple message to test!';

		// Encryption
		$ciphertext = $encrypter->encrypt($plainText);

		echo "Encrypted String: ". $ciphertext;

		echo "<br/><br/>";

		// Decryption
		echo "Decrypted String: ". $encrypter->decrypt($ciphertext);
	}
}
  • $encrypter = \Config\Services::encrypter(); – Loading service
  • $encrypter->encrypt($plainText); – Value encryption
  • $encrypter->decrypt($ciphertext); – Value decryption

Create Route

Open Routes.php from /app/Config folder and add this route into that file.

// ...

$routes->get("encryption-service", "SiteController::index");

//...

Start Development Server

Back to terminal and run this spark command.

$ php spark serve

URL: http://localhost:8080/encryption-service

When we open this URL first, we will get an error i.e about encryption key value error.

How To Fix

Open Encryption.php file from /app/Config folder and search for $key variable.

Initially, this variable will have empty value. So while using Encryption service we must pass a key into it.

Say public $key = ‘1234567890’;

Back to browser and reload URL.

Successfully, we can see the result of encryption and decryption methods.


Alternative Method – Encryption Config Settings

While using this encryption service, dynamically we can assign value of $key, $driver etc. No need to go inside Encryption.php file and do manual settings.

$config  = new \Config\Encryption(); 

$config->key    = 'aBigsecret_ofAtleast32Characters'; 

$config->driver = 'OpenSSL'; 

$encrypter = \Config\Services::encrypter($config);

$encrypter is now available to use encrypt() and decrypt() methods.

We hope this article helped you to Complete Concept of Encryption Service in CodeIgniter 4 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.