Codeigniter Encrypt Class Library


Codeigniter Encrypt Class Library – This library provides various functions that are used to encription of data. $this->load->library(‘encrypt’); is used to load the library. Here in this tutorial, we are going to explain how to use encrypt class library.


Codeigniter encrypt class library | Load | Example

Let us understand how encrypt class works in codeigniter with examples.

Load encrypt library

First load encrpt library to use its functions, this library can be loaded simply as below-

How to load encrypt class library:

 
$this->load->library('encrypt');

Functions:-

There are following functions available in encrypt class library.

  • 1. Encode
  • 2. Decode
  • 3. Set cipher
  • 4. Set mode

1. Encode

This function performs data enryption and returns string :-

  • Parameters :
  • $string (string) : Data to encrypt.
  • $key (string) : Encryption key.
  • Returns : Encrypted string.
  • Return type : string.

EXAMPLE

Lets see the simple example of encode.

Example of encode library:-

//controller
public function encode()
	{
		$this->load->library('encrypt');
		$string = 'hello user';
                $key = 'super-secret-key';
		$encrypt = $this->encrypt->encode($string, $key);
		echo $encrypt;
	}
Codeigniter Encrypt Class Library

2. Decode

Decode function is used to decrypt an encoded data :-

  • Parameters :
  • $string (string) : String to decrypt.
  • $key (string) : Encryption key.
  • Returns : Plain-text string.
  • Return type : string.

EXAMPLE

Here is the simple example of decode.

Example of decode library:-

//controller
public function decode()
	{
		$this->load->library('encrypt');
		$string = 'x3mQepxqME5O/7vBc78RDOxfjF3UsFoPsE59sSvfAhghGjdEumpBy4Kz4OOw7rqJ0k/+aVpsmNQycYsdxMwODg==';
                $key = 'super-secret-key';
                $decrypt = $this->encrypt->decode($string, $key);
                echo $decrypt;
	}
Codeigniter Encrypt Class Library

3. Set cipher

This function permit you to set Mcrypt cipher :-

  • Parameters :
  • $cipher (int) : Valid php MCrypt cypher constant.
  • Returns : CI_Encrypt instance.
  • Return type : CI_Encrypt.

EXAMPLE

Here is the simple example of set cipher.

Example of set cipher library:-

//controller
public function setcipher()
	{
		$this->load->library('encrypt');
		echo extension_loaded('mcrypt') ? 'Yup' : 'Nope';
	}
Codeigniter Encrypt Class Library

4. Set mode

This function permit you to set Mcrypt mode :-

  • Parameters :
  • $mode (int) : Valid PHP MCrypt mode constant.
  • Returns : CI_Encrypt instance (method chaining).
  • Return type : CI_Encrypt.

EXAMPLE

Here is the simple example of set mode.

Example of set mode library:-

//controller
public function setmode()
	{
		$this->load->library('encrypt');
		$this->encrypt->set_cipher(MCRYPT_RIJNDAEL_128);
		$this->encrypt->set_mode(MCRYPT_MODE_CBC);
		$key = 'mysomekey';
		$foo = $this->encrypt->encode($key);
		echo $foo;
	}
Codeigniter Encrypt Class Library

Advertisements

Add Comment

📖 Read More