-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSAEncrypt.php
More file actions
executable file
·177 lines (150 loc) · 4.13 KB
/
RSAEncrypt.php
File metadata and controls
executable file
·177 lines (150 loc) · 4.13 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
<?php
/*
* Copyright (c) 2023. Ankio. 由CleanPHP4强力驱动。
*/
/**
* Package: library\login
* Class RSAEncrypt
* Created By ankio.
* Date : 2023/7/21
* Time : 00:33
* Description :
*/
namespace library\login;
use library\mail\phpmail\Exception;
use OpenSSLAsymmetricKey;
class RSAEncrypt
{
/**
* @var array
*/
private array $config = ['public_key' => '', 'private_key' => ''];
/**
* 获取密钥
* @return array|string[]
*/
public function getKey(): array
{
return $this->config;
}
/**
* 创建密钥
* @param array $config
* @return array
*/
public function create(array $config = []): array
{
if ($config === []) {
$config = [
"digest_alg" => "sha512",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
];
}
$res = openssl_pkey_new($config);
openssl_pkey_export($res, $private_key);
$public_key = openssl_pkey_get_details($res);
$this->config = ['public_key' => $public_key["key"], 'private_key' => $private_key];
return $this->config;
}
/**
* 初始化密钥
* @param $private_key
* @param $public_key
* @return void
*/
public function initRSAData($private_key, $public_key): void
{
$this->config['private_key'] = $private_key;
$this->config['public_key'] = $public_key;
}
/**
* 初始化密钥存储路径
* @param $private_key_filepath
* @param $public_key_filepath
* @throws Exception
*/
public function initRSAPath($private_key_filepath, $public_key_filepath): void
{
$this->config['private_key'] = $this->getContents($private_key_filepath);
$this->config['public_key'] = $this->getContents($public_key_filepath);
}
/**
* 获取指定地址的路径
* @param $file_path
* @return bool|string
* @throws Exception
*/
private function getContents($file_path): bool|string
{
if (!file_exists($file_path))
throw new Exception("指定路径的密钥文件不存在:$file_path");
return file_get_contents($file_path);
}
/**
* 公钥加密
* @param string $data
* @return null|string
*/
public function rsaPublicEncrypt(string $data = ''): ?string
{
if (!is_string($data)) {
return null;
}
return openssl_public_encrypt($data, $encrypted, $this->getPublicKey()) ? base64_encode($encrypted) : null;
}
/**
* 获取公钥
* @return false|OpenSSLAsymmetricKey
*/
private function getPublicKey()
{
$public_key = $this->config['public_key'];
return openssl_pkey_get_public($public_key);
}
/**
* 私钥解密
* @param string $encrypted
* @return string|null
*/
public function rsaPrivateDecrypt(string $encrypted = ''): ?string
{
if (!is_string($encrypted)) {
return null;
}
return (openssl_private_decrypt(base64_decode($encrypted), $decrypted, $this->getPrivateKey())) ? $decrypted:null;
}
/**
* 获取私钥
* @return false|OpenSSLAsymmetricKey
*/
private function getPrivateKey(): OpenSSLAsymmetricKey|false
{
$private_key = $this->config['private_key'];
return openssl_pkey_get_private($private_key);
}
/**
* 私钥加密
* @param string $data
* @return null|string
*/
public function rsaPrivateEncrypt(string $data = ''): ?string
{
if (!is_string($data)) {
return null;
}
return openssl_private_encrypt($data, $encrypted, $this->getPrivateKey()) ? base64_encode($encrypted) : null;
}
/**
* 公钥解密
* @param string $encrypted
* @return string|null
*/
public function rsaPublicDecrypt(string $encrypted = ''): ?string
{
if (!is_string($encrypted)) {
return null;
}
return (openssl_public_decrypt(base64_decode($encrypted), $decrypted, $this->getPublicKey())) ? $decrypted:null;
}
}