forked from kevinsandow/PBEWithMD5AndDES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DesEncryptor.php
77 lines (67 loc) · 1.85 KB
/
DesEncryptor.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
<?php
class DesEncryptor
{
protected $_key;
protected $_iv;
protected $_blocksize = 8;
protected $_encrypt;
protected $_cipher;
/**
* Creates a symmetric Data Encryption Standard (DES) encryptor object
* with the specified key and initialization vector.
*
* @param $key
* @param $iv
* @param bool $encrypt
*/
public function __construct($key, $iv, $encrypt = true)
{
$this->_key = $key;
$this->_iv = $iv;
$this->_encrypt = $encrypt;
$this->_cipher = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($this->_cipher, $this->_key, $this->_iv);
}
public function __destruct()
{
mcrypt_generic_deinit($this->_cipher);
mcrypt_module_close($this->_cipher);
}
/**
* Transforms the specified region of the specified byte array using PCKS7 padding.
* @param $text
* @return string
*/
public function transformFinalBlock($text)
{
if ($this->_encrypt)
{
$padding = $this->_blocksize - strlen($text) % $this->_blocksize;
$text .= str_repeat(pack('C', $padding), $padding);
}
$text = $this->transformBlock($text);
if (!$this->_encrypt)
{
$aPadding = array_values(unpack('C', substr($text, -1)));
$padding = $aPadding[0];
$text = substr($text, 0, strlen($text) - $padding);
}
return $text;
}
/**
* Transforms the specified region of the specified byte array.
* @param $text
* @return string
*/
public function transformBlock($text)
{
if ($this->_encrypt)
{
return mcrypt_generic($this->_cipher, $text);
}
else
{
return mdecrypt_generic($this->_cipher, $text);
}
}
}