Jumat, 06 Maret 2015

PHP Encrypt / Decrypt : Simple Class untuk Enkripsi dan Dekripsi pada Codeigniter

Untuk meningkatkan keamanan, anda bisa melakukan sebuah enkripsi data pada URL yang akan anda amankan, dengan menggunakan enkripsi pada data URL tersebut. Pada Website codeigniter anda membuat bisa mencoba membuat sebuah library, letaknya pada 
application/libraries/Encryption.php.

Fungsi pada class Encryption:
class Encryption {
var $skey = "SuPerEncKey2010"; // you can change it
public function safe_b64encode($string) {
$data = base64_encode($string);
$data = str_replace(array('+','/','='),array('-','_',''),$data);
return $data;
}

Fungsi public function yang akan digunakan untuk mendekripsi url:

public function safe_b64decode($string) {
$data = str_replace(array('-','_'),array('+','/'),$string);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr('====', $mod4);
}

return base64_decode($data);
}

public function encode($value){
if(!$value){return false;}
$text = $value;
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->skey, $text, MCRYPT_MODE_ECB, $iv);
return trim($this->safe_b64encode($crypttext));
}

public function decode($value){
if(!$value){return false;}
$crypttext = $this->safe_b64decode($value);
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->skey, $crypttext, MCRYPT_MODE_ECB, $iv);
return trim($decrypttext);
}}


pada file application/libraries/autoload.php, tambahkan load library encrption
$autoload['libraries'] = array('Encryption');
pada proses enkripsi dan dekripsi
$this->encryption->encode('Your data');
$this->encryption->decode('Your encrypted data');

Jika ada pertanyaan, silahkan berikan komentar dibawah.

Related Posts:

  • Yosemite / Mamp Pro / CodeIgniter Errors Permasalahan: Severity: Notice Message: Only variable references should be returned by reference Filename: core/Common.php Line Number: 257 permasalahan lainnya : Severity: Warning Message: Cannot modify header informat… Read More
  • PHP Object Oriented Programming for Newbies This article analyzes the reasons of using Object Oriented Programming for PHP newcomers. In his opinion anyone can found thousands of tutorials for Object Oriented PHP and Object Oriented Programming. But what about a diffe… Read More
  • Minimize your URLs in Codeigniter Don’t wait until your customer or … your browser(!) start to mention that your URL is way to big from the usually title. Well the first thing that you realize when you install Codeigntier is that the URLs are really huge,… Read More
  • CodeIgniter Autocomplete Codeigniter is a great framework for php . It is simple and already helps lot of people to write faster code. The documentation is very good with examples for all the classes – functions. But (there is always a but…!) in ec… Read More
  • grocery CRUD for codeigniter This is a codeigniter CRUD. It is fully tested in codeigniter 2.0 and in 1.7.x. It is simple and has many features . The main feauture is that a customer can easily use this crud, not only a developer!! The installati… Read More

0 komentar:

Posting Komentar