×

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

4 minutes read.

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.

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.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

6 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

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

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.

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.

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

11 minutes read.

CodeIgniter CAPTCHA Helper

A CAPTCHA (Complete Automated Public Turning test to tell Computer and Humans Apart) helper is a function that is used in web application to check whether the user is human or machine. It...

3 minutes read.

CodeIgniter Language Class

Language Class The CodeIgniter’s language class contains the various functionality that is used to access the language-specific files and text line for the purposes of internationalization. This class provides you suitability to add or...

4 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 Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the...

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

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 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 Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format. Load a Number Helper You must load a number helper file in the controller to perform...

3 minutes read.

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

7 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 Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.