encription and decription in codeigniter?

How to use Encryption and Decryption In CodeIgniter?

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

Codeigniter 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');

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

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

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.

Controllers : encryption_tutorial.php

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

<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

#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;
}

encript & decript helper function

if (! function_exists('encryptor')){

function encryptor($action,$string) {

$output = false;

$encrypt_method = "AES-256-CBC";
$secret_key = 'mahi';
$secret_iv = 'mahi786';

$key = hash('sha256', $secret_key);

$iv = substr(hash('sha256', $secret_iv), 0, 16);

if( $action == 'encrypt' ) {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
}
else if( $action == 'decrypt' ){
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}

return $output;
}
}

$decrypt_ittutorial_id = encryptor('decrypt',$this->uri->segment(2));

$encrypt_data = explode('_' ,$decrypt_ittutorial_id);

$this->data['ittutorial_id'] = $encrypt_data[0];

$this->data['document_id'] = $encrypt_data[1];

<input type="hidden" id="ittutorial_id" name="standard_id" value="<?php echo encryptor('encrypt',$ittutorial_data['ittutorial_id']);?>"/>


//##############################################


<a href="<?php echo base_url()?>/user/getinfo/<?php echo encryptor('encrypt',$id);?>">view</a>


base64_encode

The base64_encode() function is an inbuilt function in PHP which is used to Encodes data with MIME base64. MIME (Multipurpose Internet Mail Extensions) base64 is used to encode the string in base64.

base64_encode( $data )

Parameters: This function accepts single parameter $data which is mandatory. It is used to specify the string encoding.

Return value: This function returns the encoded string in base64 on success or returns False in case of failure.

<?php
$str = 'itechxpert';
echo base64_encode($str);
?>

output = R2XAI3Nmb3JHZMrcw==


base64_decode() Function

The base64_decode() is an inbuilt function in PHP which is used to Decodes data which is encoded in MIME base64.

Syntax:

string base64_decode( $data, $strict )

Parameters: This function accepts two parameter as mantioned above and described below:

$data: It is mandatory parameter which contains the encoded string.
$strict: It is an optional parameter. If this parameter is set to TRUE then the base64_decode() function will return FALSE if the input contains character from outside the base64 alphabet. Invalid characters will be silently discarded.
Return value: This function returns the decoded string on success or returns False in case of failure.

<?php
$str = 'R2XAI3Nmb3JHZMrcw==';
echo base64_decode($str);
?>

output = itechxpert

How to remove = sign from encripted string.

$base_64 = base64_encode($data);
$url_param = rtrim($base_64, '=');
// and later:
$base_64 = $url_param . str_repeat('=', strlen($url_param) % 4);
$data = base64_decode($base_64);

The base64 spec only allows = signs at the end of the string, and they are used purely as padding, there is no chance of data loss.

$id= base64_decode($this->uri->segment(3));

How to encript or decript id in url?

<?php
function encryptor($action, $string) {
$output = false;
$encrypt_method = "AES-256-CBC";
//pls set your unique hashing key
$secret_key = 'mahi';
$secret_iv = 'mahi786';
// hashdate
$key = hash('sha256', $secret_key);

// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
//do the encyption given text/string/number
if( $action == 'encrypt' ) {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
}
else if( $action == 'decrypt' ){
//decrypt the given text/string/number
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}

?>


<a href="<?php echo base_url; ?>itechxpert/home.php?ID=<?php echo encryptor('encrypt',$row->id) ; ?>">
Click Here </a>





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here