lainchan/inc/ic-encrypt.php

38 lines
1.3 KiB
PHP
Raw Normal View History

2012-12-22 11:38:49 -05:00
<?php
// Z internetow.
class Encryption {
public function safe_b64encode($string) {
$data = base64_encode($string);
$data = str_replace(array('+','/','='),array('-','_',''),$data);
return $data;
}
public function safe_b64decode($string) {
$data = str_replace(array('-','_'),array('+','/'),$string);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr('====', $mod4);
}
return base64_decode($data);
}
2012-12-22 12:16:09 -05:00
public function encode($key, $value){
2012-12-22 11:38:49 -05:00
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);
2012-12-22 12:16:09 -05:00
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
2012-12-22 11:38:49 -05:00
return trim($this->safe_b64encode($crypttext));
}
2012-12-22 12:16:09 -05:00
public function decode($key, $value){
2012-12-22 11:38:49 -05:00
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);
2012-12-22 12:16:09 -05:00
$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
2012-12-22 11:38:49 -05:00
return trim($decrypttext);
}
}
?>