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:

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

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

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

$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

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

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

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

$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

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

Pin It on Pinterest

Share This