-
Notifications
You must be signed in to change notification settings - Fork 0
/
encript.php
53 lines (46 loc) · 2.11 KB
/
encript.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php class encryption{
private $config;
public function __construct( $options=array() ){
$this->config=array_merge(
array(
'cipher' => MCRYPT_RIJNDAEL_256,
'mode' => MCRYPT_MODE_ECB,
'key' => FALSE,
'iv' => FALSE,
'size' => FALSE,
'base64' => TRUE,
'salt' => FALSE
),
$options
);
}
private function getivs( $config=object ){
$config->size=mcrypt_get_iv_size( $config->cipher, $config->mode );
$config->iv=mcrypt_create_iv( $config->size, MCRYPT_RAND );
}
public function encrypt( $data=NULL ){
$config=(object)$this->config;
$this->getivs( $config );
$data=trim( $data );
$module = mcrypt_module_open( $config->cipher, '', $config->mode, '' );
mcrypt_generic_init( $module, $config->key, $config->iv );
$output = $config->base64 ? base64_encode( mcrypt_generic( $module, $data ) ) : mcrypt_generic( $module, $data );
mcrypt_generic_deinit( $module );
mcrypt_module_close( $module );
return $output;
}
public function decrypt( $data=NULL ){
$config=(object)$this->config;
$this->getivs( $config );
mb_detect_order( 'auto' );
$encoding=mb_detect_encoding( $data );
if( !$data or is_null( $data ) or empty( $data ) or !$encoding or $data=='' or base64_decode( $data )=='' ) return FALSE;
$module = mcrypt_module_open( $config->cipher, '', $config->mode, '' );
mcrypt_generic_init( $module, $config->key, $config->iv );
$output = $config->base64 ? rtrim( mdecrypt_generic( $module, base64_decode( $data ) ),"\0" ) : rtrim( mdecrypt_generic( $module, $data ),"\0" );
mcrypt_generic_deinit( $module );
mcrypt_module_close( $module );
return urldecode( $output );
}
}
?>