|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace tinymeng\tools; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* 使用openssl实现非对称加密 |
|
7
|
|
|
* Author: JiaMeng <[email protected]> |
|
8
|
|
|
* Class Rsa |
|
9
|
|
|
*/ |
|
10
|
|
|
class RsaTool |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
private $rsaPath = './';//公钥证书路径 |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Author: JiaMeng <[email protected]> |
|
17
|
|
|
* @var null|string 私钥密码 |
|
18
|
|
|
*/ |
|
19
|
|
|
private $privkeypass = null; |
|
20
|
|
|
/** |
|
21
|
|
|
* Author: JiaMeng <[email protected]> |
|
22
|
|
|
* @var string 私钥 |
|
23
|
|
|
*/ |
|
24
|
|
|
private $_privKey; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Author: JiaMeng <[email protected]> |
|
28
|
|
|
* @var string 公钥 |
|
29
|
|
|
*/ |
|
30
|
|
|
private $_pubKey; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Rsa constructor. |
|
35
|
|
|
* @param string $path 指定密钥文件地址 |
|
36
|
|
|
* @param null $privkeypass |
|
|
|
|
|
|
37
|
|
|
* @throws \Exception |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct($path = '', $privkeypass = null) |
|
40
|
|
|
{ |
|
41
|
|
|
if ($path == '') { |
|
42
|
|
|
$path = $this->rsaPath; |
|
43
|
|
|
} |
|
44
|
|
|
if (empty($path) || !is_dir($path)) { |
|
45
|
|
|
throw new \Exception('请指定密钥文件地址目录'); |
|
46
|
|
|
} |
|
47
|
|
|
$this->rsaPath = $path; |
|
48
|
|
|
$this->privkeypass = $privkeypass; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* 创建公钥和私钥 |
|
53
|
|
|
* |
|
54
|
|
|
*/ |
|
55
|
|
|
public function createKey() |
|
56
|
|
|
{ |
|
57
|
|
|
$config = array( |
|
58
|
|
|
"digest_alg" => "sha512", |
|
59
|
|
|
"private_key_bits" => 4096, |
|
60
|
|
|
"private_key_type" => OPENSSL_KEYTYPE_RSA, |
|
61
|
|
|
); |
|
62
|
|
|
// 生成私钥 |
|
63
|
|
|
$rsa = openssl_pkey_new($config); |
|
64
|
|
|
openssl_pkey_export($rsa, $privKey, $this->privkeypass, $config); |
|
65
|
|
|
file_put_contents($this->rsaPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey); |
|
66
|
|
|
// 生成公钥 |
|
67
|
|
|
$rsaPri = openssl_pkey_get_details($rsa); |
|
68
|
|
|
$pubKey = $rsaPri['key']; |
|
69
|
|
|
file_put_contents($this->rsaPath . DIRECTORY_SEPARATOR . 'pub.key', $pubKey); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* 设置私钥 |
|
74
|
|
|
* |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setupPrivKey() |
|
77
|
|
|
{ |
|
78
|
|
|
$file = $this->rsaPath . DIRECTORY_SEPARATOR . 'priv.key'; |
|
79
|
|
|
$privKey = file_get_contents($file); |
|
80
|
|
|
$this->_privKey = openssl_pkey_get_private($privKey, $this->privkeypass); |
|
|
|
|
|
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* 设置公钥 |
|
86
|
|
|
* |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setupPubKey() |
|
89
|
|
|
{ |
|
90
|
|
|
$file = $this->rsaPath . DIRECTORY_SEPARATOR . 'pub.key'; |
|
91
|
|
|
$pubKey = file_get_contents($file); |
|
92
|
|
|
$this->_pubKey = openssl_pkey_get_public($pubKey); |
|
|
|
|
|
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* 用私钥加密 |
|
98
|
|
|
* |
|
99
|
|
|
*/ |
|
100
|
|
|
public function privEncrypt($data) |
|
101
|
|
|
{ |
|
102
|
|
|
if (!is_string($data)) { |
|
103
|
|
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
$this->setupPrivKey(); |
|
106
|
|
|
$result = openssl_private_encrypt($data, $encrypted, $this->_privKey); |
|
107
|
|
|
if ($result) { |
|
108
|
|
|
return base64_encode($encrypted); |
|
109
|
|
|
} |
|
110
|
|
|
return null; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* 私钥解密 |
|
115
|
|
|
* |
|
116
|
|
|
*/ |
|
117
|
|
|
public function privDecrypt($encrypted) |
|
118
|
|
|
{ |
|
119
|
|
|
if (!is_string($encrypted)) { |
|
120
|
|
|
return null; |
|
121
|
|
|
} |
|
122
|
|
|
$this->setupPrivKey(); |
|
123
|
|
|
$encrypted = base64_decode($encrypted); |
|
124
|
|
|
$result = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey); |
|
125
|
|
|
if ($result) { |
|
126
|
|
|
return $decrypted; |
|
127
|
|
|
} |
|
128
|
|
|
return null; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* 公钥加密 |
|
133
|
|
|
* |
|
134
|
|
|
*/ |
|
135
|
|
|
public function pubEncrypt($data) |
|
136
|
|
|
{ |
|
137
|
|
|
if (!is_string($data)) { |
|
138
|
|
|
return null; |
|
139
|
|
|
} |
|
140
|
|
|
$this->setupPubKey(); |
|
141
|
|
|
$result = openssl_public_encrypt($data, $encrypted, $this->_pubKey); |
|
142
|
|
|
if ($result) { |
|
143
|
|
|
return base64_encode($encrypted); |
|
144
|
|
|
} |
|
145
|
|
|
return null; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Description: 公钥解密 |
|
150
|
|
|
* Author: JiaMeng <[email protected]> |
|
151
|
|
|
* Updater: |
|
152
|
|
|
* @param $crypted |
|
153
|
|
|
* @return null |
|
154
|
|
|
*/ |
|
155
|
|
|
public function pubDecrypt($crypted) |
|
156
|
|
|
{ |
|
157
|
|
|
if (!is_string($crypted)) { |
|
158
|
|
|
return null; |
|
159
|
|
|
} |
|
160
|
|
|
$this->setupPubKey(); |
|
161
|
|
|
$crypted = base64_decode($crypted); |
|
162
|
|
|
$result = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey); |
|
163
|
|
|
if ($result) { |
|
164
|
|
|
return $decrypted; |
|
165
|
|
|
} |
|
166
|
|
|
return null; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|