-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRSA.php
174 lines (137 loc) · 4.66 KB
/
RSA.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
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
<?php
namespace ZhenMu\Support\Utils;
class RSA
{
public static function generate($config = [])
{
$config = array_merge(array(
'private_key_bits' => 2048,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
), $config);
$privateKeyObj = openssl_pkey_new($config);
$publicKey = openssl_pkey_get_details($privateKeyObj)['key'];
openssl_pkey_export($privateKeyObj, $privateKey);
return [
'publicKey' => $publicKey,
'privateKey' => $privateKey,
];
}
public static function singleLinePublicKey($publicKey)
{
$string = str_replace([
"-----BEGIN PUBLIC KEY-----\n",
"-----END PUBLIC KEY-----",
], '', $publicKey);
$stringArray = explode("\n", $string);
$result = implode('', $stringArray);
return $result;
}
public static function singleLinePrivateKey($privateKey)
{
$string = str_replace([
"-----BEGIN PRIVATE KEY-----\n",
"-----END PRIVATE KEY-----",
], '', $privateKey);
$stringArray = explode("\n", $string);
$result = implode('', $stringArray);
return $result;
}
public static function normalPublicKey($publicKey)
{
$fKey = "-----BEGIN PUBLIC KEY-----\n";
$len = strlen($publicKey);
for ($i = 0; $i < $len;) {
$fKey = $fKey . substr($publicKey, $i, 64) . "\n";
$i += 64;
}
$fKey .= "-----END PUBLIC KEY-----";
return $fKey;
}
public static function normalPrivateKey($privateKey)
{
$fKey = "-----BEGIN PRIVATE KEY-----\n";
$len = strlen($privateKey);
for ($i = 0; $i < $len;) {
$fKey = $fKey . substr($privateKey, $i, 64) . "\n";
$i += 64;
}
$fKey .= "-----END PRIVATE KEY-----";
return $fKey;
}
public static function chunkEncrypt($data, $publicKey, $keySize = 2048)
{
if (!$data) {
return null;
}
if (!is_string($data)) {
$data = json_encode($data, JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}
if (str_contains($publicKey, "PUBLIC") === false) {
$publicKey = RSA::normalPublicKey($publicKey); // 格式化公钥为标准的 public key
}
$plaintext = $data;
$chunkSize = $keySize / 8 - 11;
$output = "";
while ($plaintext) {
$chunk = substr($plaintext, 0, $chunkSize);
$plaintext = substr($plaintext, $chunkSize);
openssl_public_encrypt($chunk, $encrypted, $publicKey);
$output .= $encrypted;
}
return Str::stringToHex($output);
}
public static function chunkDecrypt($data, $privateKey, $keySize = 2048)
{
if (!$data) {
return null;
}
if (str_contains($privateKey, "PRIVATE") === false) {
$privateKey = RSA::normalPrivateKey($privateKey); // 格式化私钥为标准的 private key
}
$output = Str::hexToString($data);
$plaintext = "";
while ($output) {
$chunk = substr($output, 0, $keySize / 8);
$output = substr($output, $keySize / 8);
openssl_private_decrypt($chunk, $decrypted, $privateKey);
$plaintext .= $decrypted;
}
return $plaintext;
}
public static function encrypt($data, $privateKey)
{
if (!$data) {
return null;
}
if (!is_string($data)) {
$data = json_encode($data, JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}
if (str_contains($privateKey, "PRIVATE") === false) {
$privateKey = RSA::normalPrivateKey($privateKey); // 格式化私钥为标准的 private key
}
$res = openssl_private_encrypt($data, $encrypted, $privateKey);
if (!$res) {
return false;
}
return Str::stringToHex($encrypted);
}
public static function decrypt($data, $publicKey)
{
if (!$data) {
return null;
}
if (!is_string($data)) {
return false;
}
if (str_contains($publicKey, "PUBLIC") === false) {
$publicKey = RSA::normalPublicKey($publicKey); // 格式化公钥为标准的 public key
}
// $openssl_pub = openssl_pkey_get_public($publicKey); // 不知道作用,未使用
// 验签
$resArr = openssl_public_decrypt(Str::hexToString($data), $decrypted, $publicKey);
if (!$resArr) {
return false;
}
return $decrypted;
}
}