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