×

CodeIgniter Encrypt Class

The Codeigniter provides an encrypt class used to protect confidential data stored on computer systems or data transmitted over a network. It provides two-way data encryption techniques such as Mcrypt PHP extension and Open SSL.

What is Key?

In cryptography, a key is an information that implements an algorithm to convert a string or a plaintext into encrypted text, and it also allows the conversion of an encrypted text into decrypted text. In fact, it allows you to choose a key that can help to decode the data that was encrypted with that key.

Load an Encrypt Class

Before using the encrypted class in Codeigniter, you must load in the controller file.

Syntax:

$this -> load-> library ( ‘encrypt’ );

There are various functions available in the encrypt class library.

  1. encode(): As the name defines, an encode () function is used to encrypt a string or confidential data with the use of an encryption key.

Syntax

encode( $string [, $key = ‘’ ]);

It has two parameters:

$string: In this field, you can pass confidential data or a string that can be encrypted.

$key: It defines a secret key, that helps to convert a simple string or messages into a secret format.

Create a controller file Tests_encrypt.php and save it in application/controller/ Tests_encrypt.php. After that, write the following program in the controller file.

Tests_encrypt.php

<?php
 class Tests_encrypt extends CI_Controller {
   function encode()
   {    
  echo "<title> Tutorial and Example </title>";
       $this->load->library('encrypt');
      $data = 'Tutorial and Example is the good plateform for the learner';
     $key ="tutorialand example";
 $encrypted_data = $this->encrypt->encode($data, $key);
 echo "<pre>$encrypted_data </pre>";
   }
 } 
 ?> 

Now execute the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/ Tests_encrypt/encode function; it shows the output, as shown below.

Encrypt Class
  • decode(): The decode() function helps to decode an encrypted string or text in a readable format or a plain text with the use of keys.

Syntax

decode ( $string [, $key = ‘’ ]);

$string: It defines a string or data that can be decrypted.

$key: It uses the same key that you have used in encryption.

Create a controller file Tests_encrypt.php and save it in application/controller/ Tests_encrypt.php. After that, write the following program in the controller file.

Tests_encrypt.php

<?php
 class Tests_encrypt extends CI_Controller {
 function decode()
   {     echo "<title> Tutorial and Example </title>";
       $this->load->library('encrypt');
      $msg = 'XYxlpf+7+kbrvmfQ53LZ0Z7iwEpMr4TeZomLBWeaKowhDd3WoVFyyuSOBkTCB8oHwdHUuIWK9evQwl3KjzSnsJFrQLM3xTEN338gEcPmrbXQvSQXq2Al7x+xo6jlabu8';
      $key ="tutorialand example";
 $decrypted_data = $this->encrypt->decode($msg, $key);
 echo "<cite>$decrypted_data </cite>";
   }
 }
 ?> 

Now execute the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/ Tests_encrypt/decode function; it shows the output, as shown below.

Encrypt Class
  • set_cipher(): As the function defines, it sets a PHP MCrypt cipher to return a CI_Encrypt object. By default, it uses PHP MCRYPT_RIJNDAEL_256.

Syntax

set_cipher ($cipher);
 $cipher: It contains valid PHP Mcrypt cipher. 

Create a controller file Tests_encrypt.php and save it in application/controller/ Tests_encrypt.php. After that, write the following program in the controller file.

Tests_encrypt.php

<?php
 class Tests_encrypt extends CI_Controller {
 function set_cipher()
 {     echo "<title> Tutorial and Example </title>";
   $this->load->library('encrypt');
   $data=$this->encrypt->set_cipher(MCRYPT_BLOWFISH);
   print_r($data);
   echo "<br/ >You can also define it manually like ";
   echo extension_loaded('mcrypt') ? 'Hello' : 'Friends';
 }
 }
 ?> 

Now execute the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/Tests_encrypt/set_cipher function; it shows the output, as shown below.

Encrypt Class
  • set_mode(): A set_mode() function is used to set an Mcrypt mode for encrypting the data. By default, it uses MCRYPT_MODE_CBC.

Syntax

set_mode ($mode);

$mode: It represents a valid PHP Mcrypt mode for securing data.

Create a controller file Tests_encrypt.php and save it in application/controller/ Tests_encrypt.php. After that, write the following program in the controller file.

Tests_encrypt.php

<?php
 class Tests_encrypt extends CI_Controller {
 function set_mode()
 {     echo "<title> Tutorial and Example </title>";
     $this->load->library('encrypt');
     $info =$this->encrypt->set_mode(MCRYPT_MODE_CFB);
     print_r($info);
 }
 }
 ?> 

Now execute the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/ Tests_encrypt/set_mode function; it shows the output, as shown below.

Encrypt Class

Related Topics

CodeIgniter URI Class

URI Class The CodeIgniter contains a URI class that is used to retrieve information from URI strings. It also provides suitability to get information from the re-routed segments. The system automatically initializes this...

6 minutes read.

Structure of Codeigniter

The File structure of CodeIgniter is divided into three parts: ApplicationSystemuser_guide Application -: As the name represents Application, which contains all the main parts of the project like a controller, libraries, view, models, and others....

3 minutes read.

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 minutes read.

CodeIgniter Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

4 minutes read.

CodeIgniter Inflector Helper

The inflector helper file contains some predefined function that allows users to change their English words into plural, singular, and camel case, etc. Loading an Inflector Helper Before using an inflector helper function, you must...

3 minutes read.

CodeIgniter URL Helper

The helper file contains some predefined functions that are used to work with URL, such as generate the current file path, transfer control, redirect, create a link, etc. Load the URL Helper Before using...

7 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.

CodeIgniter Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user...

3 minutes read.

CodeIgniter Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 minutes read.

Directory Helper Codeigniter

Directory Helper CodeIgniter: provides a directory helper function that works with the directory. It shows the structure of the file, hidden file in the folder, and the folder in...

3 minutes read.

How to remove an index.php file from the Codeigniter URLs?

As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a...

2 minutes read.

CodeIgniter Routes

The Routes are used to handle URL requests on the browser. It matches the URL request to the predefined routes in CodeIgniter. Therefore, a file must be predefined as a routes.php file to...

6 minutes read.

CodeIgniter Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

CodeIgniter Views

A View is a presentational part which shows the information to the users. It is a complex web page which contains navbar, header, footer, sidebar, etc. A view file cannot be called directly...

5 minutes read.

CodeIgniter Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.

CodeIgniter Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

CodeIgniter Creating Library

As we know, CodeIgniter has a large collection of library functions that are stored in the system/libraries folder. CodeIgniter also provides some additional functionality in library files. For example, if you want to...

3 minutes read.

CodeIgniter User Agent Class

User Agent Class The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used...

4 minutes read.