1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Encrypt/decrypt data to a format compatible with MySQL aes_encrypt() & aes_decrypt() functions. |
4
|
|
|
* |
5
|
|
|
* @author Bob Fanger <[email protected]> |
6
|
|
|
* @author Anne Jan Brouwer <[email protected]> |
7
|
|
|
* @author Govert Verschuur <[email protected]> |
8
|
|
|
* @author Renan Martins Pimentel <[email protected]> |
9
|
|
|
* @copyright 2016 NoProtocol |
10
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
11
|
|
|
* |
12
|
|
|
* @version 2.0.1 |
13
|
|
|
* |
14
|
|
|
* @link http://www.smashingmagazine.com/2012/05/replicating-mysql-aes-encryption-methods-with-php/ |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace NoProtocol\Encryption\MySQL\AES; |
18
|
|
|
|
19
|
|
|
class Crypter |
20
|
|
|
{ |
21
|
|
|
protected $method; |
22
|
|
|
protected $key; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Crypter constructor. |
26
|
|
|
* |
27
|
|
|
* @param $seed |
28
|
|
|
* @param string $method default AES-128-ECB |
29
|
|
|
*/ |
30
|
6 |
|
public function __construct($seed, $method = 'AES-128-ECB') |
31
|
|
|
{ |
32
|
6 |
|
$this->method = $method; |
33
|
6 |
|
$this->key = $this->generateKey($seed); |
34
|
6 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Encrypts the data. |
38
|
|
|
* |
39
|
|
|
* @since 2.0 |
40
|
|
|
* |
41
|
|
|
* @param string $data A string of data to encrypt. |
42
|
|
|
* |
43
|
|
|
* @return string (binary) The encrypted data |
44
|
|
|
*/ |
45
|
3 |
|
public function encrypt($data) |
46
|
|
|
{ |
47
|
3 |
|
$chiperIvLength = openssl_cipher_iv_length($this->method); |
48
|
3 |
|
$iv = ''; |
49
|
3 |
|
if ($chiperIvLength > 0) { |
50
|
|
|
$iv = openssl_random_pseudo_bytes($chiperIvLength); |
51
|
|
|
} |
52
|
3 |
|
$padValue = 16 - (strlen($data) % 16); |
53
|
|
|
|
54
|
3 |
|
return openssl_encrypt( |
55
|
3 |
|
str_pad($data, intval(16 * (floor(strlen($data) / 16) + 1)), chr($padValue)), |
56
|
3 |
|
$this->method, |
57
|
3 |
|
$this->key, |
58
|
3 |
|
OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, |
59
|
3 |
|
$iv |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Decrypts the data. |
65
|
|
|
* |
66
|
|
|
* @since 2.0 |
67
|
|
|
* |
68
|
|
|
* @param string $data A (binary) string of encrypted data |
69
|
|
|
* |
70
|
|
|
* @return string Decrypted data |
71
|
|
|
*/ |
72
|
3 |
|
public function decrypt($data) |
73
|
|
|
{ |
74
|
3 |
|
$data = openssl_decrypt( |
75
|
3 |
|
$data, |
76
|
3 |
|
$this->method, |
77
|
3 |
|
$this->key, |
78
|
3 |
|
OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING |
79
|
|
|
); |
80
|
|
|
|
81
|
3 |
|
return rtrim($data, "\x00..\x10"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create and set the key used for encryption. |
86
|
|
|
* |
87
|
|
|
* @since 2.0 |
88
|
|
|
* |
89
|
|
|
* @param string $seed The seed used to create the key. |
90
|
|
|
* |
91
|
|
|
* @return string (binary) the key to use in the encryption process. |
92
|
|
|
*/ |
93
|
6 |
|
protected function generateKey($seed) |
94
|
|
|
{ |
95
|
6 |
|
$key = str_repeat(chr(0), 16); |
96
|
6 |
|
for ($i = 0, $len = strlen($seed); $i < $len; $i++) { |
97
|
6 |
|
$key[$i % 16] = $key[$i % 16] ^ $seed[$i]; |
98
|
|
|
} |
99
|
|
|
|
100
|
6 |
|
return $key; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|