Data encryption and decryption is nothing but just converting a plain text into something that appears to be random and meaningless and vice verse.

Generally this encryption is opted by the developers to make the data secured like for password, URL, credit card numbers and so on.

If you are using CodeIgniter than to encrypt the data becomes much more easier as compared to native PHP code.

As C.I. provides its own encryption class which you can use to encrypt or decrypt the data.

To initialize the encryption class one must have to load the library as shown below:

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

As soon as it gets loaded, then Encrypt library object will be available using: $this->encrypt

But before doing all these process one must require to set up the encryption key. This Key is nothing but an information that controls the cryptographic process and allow encrypted string to get decrypted.

This key should be any random string but not a simple plain text and should be 32 characters in length (128 bits).

For setting up your key, go to application/config/config.php, open the file and set:

 $config['encryption_key'] = "YOUR KEY"; 

Below we have created an example to show, how to encode and decode the data.

To encode the code we have used below syntax:

$this->encrypt->encode() // Performs the data encryption and returns it as a string

To decode the code use:

$this->encrypt->decode() // Decrypts an encoded string.

If you wish you can copy the codes and can try at your end locally or can even download the files

 


 

Encryption in CodeIgniter

 


Tutorial Scripts in detail

Below are the details of the code used in this tutorial with proper explanation.

Controllers : encryption_tutorial.php

copy the below code in your controller.

<?php

class Encryption_Tutorial extends CI_Controller {

public function __construct() {
parent:: __construct();
// Load form helper
$this->load->helper('form');
// Load encryption library
$this->load->library('encrypt');
// Load form validation library
$this->load->library('form_validation');
}

// Show form
public function index() {
$this->load->view('show_form');
}

// Encode message
public function key_encoder() {

// Check for validation
$this->form_validation->set_rules('key', 'Message', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('show_form');
} else {
$key = $this->input->post('key');
// Encoding message
$data['encrypt_value'] = $this->encrypt->encode($key);
$this->load->view('show_form', $data);
}
}

// Decode encrypted message
public function key_decoder() {

$encrypt_key = $this->input->post('encrypt_key');
// Decode message
$data['decrypt_value'] = $this->encrypt->decode($encrypt_key);
$this->load->view('show_form', $data);
}

}

?>

 

Views : show_form.php

Copy the below code in your view.

<html>
<head>
<title>Encryption In CodeIgniter</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css">
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
</head>
<body>
<div id="main">
<div id="show_form">
<h2>Encryption In CodeIgniter</h2>
<?php
echo form_open('encryption_tutorial/key_encoder');
echo "<div class='error_msg'>";
echo validation_errors();
echo "</div>";
echo form_label('Enter your Message');
$data = array(
'name' => 'key',
'placeholder' => 'Please Enter a Message'
);
echo form_input($data);
echo form_submit('submit', 'Encode');
echo form_close();
if (isset($decrypt_value) && $decrypt_value != NULL) {
echo form_fieldset('Decrypted Message', "class='result_decode'");
echo "<b>" . $decrypt_value . "</b>";
echo form_fieldset_close();
}
?>
</div>
</div>
<?php
if (isset($encrypt_value) && $encrypt_value != NULL) {
echo form_fieldset('Encrypted Message', "class='result_encode'");
echo "<b>" . $encrypt_value . "</b>";
echo form_fieldset_close();
echo "<div class='decode_form'>";
echo form_open('encryption_tutorial/key_decoder');
echo form_label('Decode Encrypted Message');
$data = array(
'name' => 'encrypt_key',
'value' => $encrypt_value
);
echo form_input($data);
echo form_submit('submit', 'Decode');
echo form_close();
echo "</div>";
}
?>

 

CSS : style.css

Styling HTML Elements.



#main{
width:960px;
margin:50px auto;
font-family:raleway;
}
h2{
background-color: #FEFFED;
text-align:center;
border-radius: 10px 10px 0 0;
margin: -10px -40px;
padding: 30px;
}
hr{
border:0;
border-bottom:1px solid #ccc;
margin: 10px -40px;
margin-bottom: 30px;
}
#show_form{
width:300px;
float: left;
border-radius: 10px;
font-family:raleway;
border: 2px solid #ccc;
padding: 10px 40px 25px;
margin-top: 10px;
}
.result_encode{
position: absolute;
width: auto;
border-radius: 10px;
font-family: raleway;
border: 2px solid #ccc;
top: 400px;
padding: 30px;
word-wrap:break-word;
}
.result_decode{
position: absolute;
width: 500px;
border-radius: 10px;
font-family: raleway;
border: 2px solid #ccc;
top: 400px;
left: 302px;
padding: 30px;
word-wrap:break-word;
}
.decode_form{
position: absolute;
width: 300px;
float: right;
border-radius: 10px;
font-family: raleway;
border: 2px solid #ccc;
top: 540px;
left: 325px;
padding: 50px 40px 25px;
}
input[type=text]{
width:100%;
padding: 10px;
margin-top: 8px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
font-family:raleway;
background-color: #FEFFED;
}

input[type=submit]{
width: 100%;
background-color:#FFBC00;
color: white;
border: 2px solid #FFCB00;
padding: 10px;
font-size:20px;
cursor:pointer;
border-radius: 5px;
margin-bottom: 15px;
}
.error_msg{
color:red;
font-size: 16px;
}

 

Conclusion:

This was all about how Encryption work  In CodeIgniter. Hope you like it, keep reading our other blogs