-
Notifications
You must be signed in to change notification settings - Fork 3
/
KdbCrypt.php
107 lines (93 loc) · 2.5 KB
/
KdbCrypt.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* @author Lutz-Peter Hooge
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LESSER GENERAL PUBLIC LICENSE
* @package libKeePHPass
*/
class KdbCrypt{
protected $mcrypt = null;
protected $mode = null;
protected $algo = null;
protected $iv = null;
/**
* @param string $key encryption key
* @param string $iv initialisation vektor, if null an empty IV (null-bytes) is used
* @param string $mode any of the MCRYPT_MODE_* constants
* @param string $algo MCRYPT algorithm, like MCRYPT_RIJNDAEL_128, MCRYPT_TWOFISH
*/
public function __construct($key, $iv=null, $mode=MCRYPT_MODE_CBC, $algo=MCRYPT_RIJNDAEL_128){
$this->init($key, $iv, $mode, $algo);
}
public function __destruct(){
$this->deinit();
}
public function test(){
if(mcrypt_enc_self_test($this->mcrypt) == true){
throw new Exception("mcrypt self test failed");
}
}
public function encrypt($str){
return mcrypt_generic($this->mcrypt, $str);
}
public function decrypt($str){
return mdecrypt_generic($this->mcrypt, $str);
}
/**
* decrypt the string and remove PKCS7 padding
*
* @param string $str
* @return string
*/
public function padDecrypt($str){
$dec = $this->decrypt($str);
$strlen = strlen($dec);
$pad = $dec{$strlen-1};
$padlen = ord($pad);
for($i=1;$i<=$padlen; $i++){
if($dec{$strlen-$i} != $pad){
throw new Exception("corrupted data: padding error ");
}
}
return substr($dec,0,$strlen-$padlen);
}
/**
* add PKCS7 padding and encrypt the string
*
* @param string $str
* @return string
*/
public function padEncrypt($str){
$str = (string) $str;
$padding = 16 - strlen($str) % 16;
$str .= str_repeat(chr($padding), $padding);
return $this->encrypt($str);
}
protected function init($key, $iv=null, $mode=MCRYPT_MODE_CBC, $algo=MCRYPT_RIJNDAEL_128){
$mcrypt = mcrypt_module_open($algo,null, $mode, null);
if($iv===null){
$iv = str_repeat("\0", mcrypt_enc_get_iv_size($mcrypt));
}
$init_ok = mcrypt_generic_init($mcrypt, $key, $iv);
if($init_ok !== 0){
throw new Exception("mcrypt init failed");
}
if(mcrypt_enc_self_test($mcrypt) == true){
throw new Exception("mcrypt self test failed");
}
$this->mcrypt = $mcrypt;
$this->mode = $mode;
$this->algo = $algo;
$this->iv = $iv;
}
protected function deinit(){
if(!$this->mcrypt){
return;
}
mcrypt_generic_deinit($this->mcrypt);
mcrypt_module_close($this->mcrypt);
$this->mcrypt = null;
$this->mode = null;
$this->algo = null;
$this->iv = null;
}
}