Encryption Service in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 5 minutes
2,515 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.

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system.

To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

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

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.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# 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.

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.

Find More on CodeIgniter 4 here