Rsa.php
4.5 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace tinymeng\tools;
/**
* 使用openssl实现非对称加密
* Author: JiaMeng <666@majiameng.com>
* Class Rsa
*/
class Rsa
{
private $rsaPath = './';//公钥证书路径
/**
* Author: JiaMeng <666@majiameng.com>
* @var null|string 私钥密码
*/
private $privkeypass = null;
/**
* Author: JiaMeng <666@majiameng.com>
* @var string 私钥
*/
private $_privKey;
/**
* Author: JiaMeng <666@majiameng.com>
* @var string 公钥
*/
private $_pubKey;
/**
* Rsa constructor.
* @param string $path 指定密钥文件地址
* @param null $privkeypass
* @throws \Exception
*/
public function __construct($path = '', $privkeypass = null)
{
if ($path == '') {
$path = $this->rsaPath;
}
if (empty($path) || !is_dir($path)) {
throw new \Exception('请指定密钥文件地址目录');
}
$this->rsaPath = $path;
$this->privkeypass = $privkeypass;
}
/**
* 创建公钥和私钥
*
*/
public function createKey()
{
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
// 生成私钥
$rsa = openssl_pkey_new($config);
openssl_pkey_export($rsa, $privKey, $this->privkeypass, $config);
file_put_contents($this->rsaPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey);
// 生成公钥
$rsaPri = openssl_pkey_get_details($rsa);
$pubKey = $rsaPri['key'];
file_put_contents($this->rsaPath . DIRECTORY_SEPARATOR . 'pub.key', $pubKey);
}
/**
* 设置私钥
*
*/
public function setupPrivKey()
{
$file = $this->rsaPath . DIRECTORY_SEPARATOR . 'priv.key';
$privKey = file_get_contents($file);
$this->_privKey = openssl_pkey_get_private($privKey, $this->privkeypass);
return true;
}
/**
* 设置公钥
*
*/
public function setupPubKey()
{
$file = $this->rsaPath . DIRECTORY_SEPARATOR . 'pub.key';
$pubKey = file_get_contents($file);
$this->_pubKey = openssl_pkey_get_public($pubKey);
return true;
}
/**
* 用私钥加密
*
*/
public function privEncrypt($data)
{
if (!is_string($data)) {
return null;
}
$this->setupPrivKey();
$result = openssl_private_encrypt($data, $encrypted, $this->_privKey);
if ($result) {
return base64_encode($encrypted);
}
return null;
}
/**
* 私钥解密
*
*/
public function privDecrypt($encrypted)
{
if (!is_string($encrypted)) {
return null;
}
$this->setupPrivKey();
$encrypted = base64_decode($encrypted);
$result = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);
if ($result) {
return $decrypted;
}
return null;
}
/**
* 公钥加密
*
*/
public function pubEncrypt($data)
{
if (!is_string($data)) {
return null;
}
$this->setupPubKey();
$result = openssl_public_encrypt($data, $encrypted, $this->_pubKey);
if ($result) {
return base64_encode($encrypted);
}
return null;
}
/**
* Description: 公钥解密
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @param $crypted
* @return null
*/
public function pubDecrypt($crypted)
{
if (!is_string($crypted)) {
return null;
}
$this->setupPubKey();
$crypted = base64_decode($crypted);
$result = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);
if ($result) {
return $decrypted;
}
return null;
}
}
/**
$privkeypass = '95920180927';//私钥密码
$rsa = new Rsa('/data/majiameng.com/public/rsa/',$privkeypass);
//私钥加密,公钥解密
echo "待加密数据:segmentfault.com\n";
$pre = $rsa->privEncrypt("segmentfault.com");
echo "加密后的密文:\n" . $pre . "\n";
$pud = $rsa->pubDecrypt($pre);
echo "解密后数据:" . $pud . "\n";
//公钥加密,私钥解密
echo "待加密数据:segmentfault.com\n";
$pue = $rsa->pubEncrypt("segmentfault.com");
echo "加密后的密文:\n" . $pue . "\n";
$prd = $rsa->privDecrypt($pue);
echo "解密后数据:" . $prd;
*/