Last active
August 29, 2015 14:18
-
-
Save AnowarCST/251e35793bf0f1cb3bbf to your computer and use it in GitHub Desktop.
Encryption library Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Encryption{ | |
var $defskey = "NibidAlo"; // Encryption Key | |
public function safe_b64encode($string) { | |
$data = base64_encode($string); | |
$data = str_replace(array('+','/','='),array('-','_',''),$data); | |
return urlencode($data); | |
} | |
public function safe_b64decode($string) { | |
$data = urldecode($string); | |
$data = str_replace(array('-','_'),array('+','/'),$string); | |
$mod4 = strlen($data) % 4; | |
if ($mod4) { | |
$data .= substr('====', $mod4); | |
} | |
return base64_decode($data); | |
} | |
public function encode($value, $skey=""){ | |
if(!$value){return false;} | |
if( function_exists( 'mcrypt_module_open' ) ){ | |
if(strlen($skey)==0) $skey=$this->defskey; | |
//echo 'ok- mcrypt enabled'; | |
$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, $skey, $text, MCRYPT_MODE_ECB, $iv); | |
return $this->safe_b64encode($crypttext); | |
}else{ | |
return $this->safe_b64encode($value); | |
} | |
} | |
public function decode($value, $skey=""){ | |
if(!$value){return false;} | |
if(strlen($skey)==0) $skey=$this->defskey; | |
if( function_exists( 'mcrypt_module_open' ) ){ | |
//echo 'ok- mcrypt enabled'; | |
$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, $skey, $crypttext, MCRYPT_MODE_ECB, $iv); | |
return trim($decrypttext); | |
}else{ | |
return $this->safe_b64decode($value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment