×

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 Helpers

The Helpers (functions) are stored in the Helper file. It helps CodeIgniter to perform different tasks. The helper file is the collection of many functions that is used to perform specific task. It...

3 minutes read.

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

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

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

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

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.

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.

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 HTML Helper

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc. Loading the HTML Helper Before using the html helper function, you must load the...

7 minutes read.

CodeIgniter Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 minutes read.

CodeIgniter Libraries

The library is an important part of the CodeIgniter framework. It has a large collection of libraries files that are used to increase the performance of an application. By default, all CodeIgniter libraries...

4 minutes read.

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

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

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 Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

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