1+ <?php
2+ namespace niklaslu ;
3+ /**
4+ * Created by PhpStorm.
5+ * User: Administrator
6+ * Date: 2017/4/20
7+ * Time: 11:46
8+ *
9+ *
10+ * keyType file :文件 str:直接传递字符串
11+ *
12+ *
13+ */
14+ class Rsa
15+ {
16+ // 私钥字符串
17+ protected $ private_key_str = '' ;
18+
19+ // 公钥字符串
20+ protected $ public_key_str = '' ;
21+
22+ // 私钥文件路径
23+ protected $ private_key_path = '' ;
24+
25+ // 公钥文件路径
26+ protected $ public_key_path = '' ;
27+
28+ protected $ error = '' ;
29+
30+ protected $ keyType = 'file ' ;
31+
32+ public function __construct ($ config = null ){
33+
34+ if (isset ($ config ['type ' ])){
35+ $ this ->keyType = $ config ['type ' ] ? $ config ['type ' ] : $ this ->keyType ;
36+ }
37+
38+ if ($ this ->keyType == 'file ' ){
39+ // 获取默认public key private key
40+ if (isset ($ config ['public_key_path ' ]) && isset ($ config ['private_key_path ' ])){
41+ $ this ->public_key_path = $ config ['public_key_path ' ];
42+ $ this ->private_key_path = $ config ['private_key_path ' ];
43+ }else {
44+ $ this ->public_key_path = dirname (__FILE__ ) . '/key/rsa_public_key.pem ' ;
45+ $ this ->private_key_path = dirname (__FILE__ ) . '/key/rsa_private_key.pem ' ;
46+ }
47+
48+ if ($ this ->private_key_path ){
49+ $ this ->private_key_str = $ this ->getKeyStr ($ this ->private_key_path );
50+ }
51+ if ($ this ->public_key_path ){
52+ $ this ->public_key_str = $ this ->getKeyStr ($ this ->public_key_path );
53+ }
54+ }elseif ($ this ->keyType == 'str ' ){
55+ $ this ->private_key_str = isset ($ config ['private_key_str ' ]) ? $ config ['private_key_str ' ] : $ this ->private_key_str ;
56+ $ this ->public_key_str = isset ($ config ['public_key_str ' ]) ? $ config ['public_key_str ' ] : $ this ->public_key_str ;
57+ }
58+
59+ return $ this ;
60+
61+ }
62+
63+ public function setPublicKeyStr ($ publicKeyStr ){
64+
65+ $ this ->public_key_str = $ publicKeyStr ;
66+ return $ this ;
67+ }
68+
69+ public function setPrivateKeyStr ($ privateKeyStr ){
70+
71+ $ this ->private_key_str = $ privateKeyStr ;
72+ return $ this ;
73+ }
74+
75+ /**
76+ * 私钥加密
77+ * @param $data
78+ * @param string $encrypted
79+ * @return bool|string
80+ */
81+ public function encryptByPrivate ($ data , $ encrypted = '' ){
82+
83+ $ privateKey = $ this ->getPrivateKeyStr ();
84+
85+ if (!$ privateKey ){
86+ return false ;
87+ }
88+
89+ // data为数组的时候转成json
90+ if (!is_string ($ data )){
91+ $ data = json_encode ($ data );
92+ }
93+ //私钥加密
94+ openssl_private_encrypt ($ data ,$ encrypted ,$ privateKey );
95+
96+ //加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
97+ $ encrypted = base64_encode ($ encrypted );
98+
99+ return $ encrypted ;
100+
101+ }
102+
103+ /**
104+ * 公钥解密
105+ * @param $data
106+ * @param string $decrypted
107+ * @return bool|string
108+ */
109+ public function decryptByPublic ($ data , $ decrypted = '' ){
110+
111+ $ publicKey = $ this ->getPublicKeyStr ();
112+ if (!$ publicKey ){
113+ return false ;
114+ }
115+
116+ $ data = base64_decode ($ data );
117+
118+ //私钥加密的内容通过公钥可用解密出来
119+ openssl_public_decrypt ($ data ,$ decrypted , $ publicKey );
120+
121+ return $ decrypted ;
122+
123+ }
124+
125+ /**
126+ * 公钥加密
127+ * @param $data
128+ * @param string $encrypted
129+ * @return bool|string
130+ */
131+ public function encryptByPublic ($ data , $ encrypted = '' ){
132+
133+ $ publicKey = $ this ->getPublicKeyStr ();
134+ if (!$ publicKey ){
135+ return false ;
136+ }
137+ // data为数组的时候转成json
138+ if (!is_string ($ data )){
139+ $ data = json_encode ($ data );
140+ }
141+ // 公钥加密
142+ openssl_public_encrypt ($ data , $ encrypted , $ publicKey );
143+ $ encrypted = base64_encode ($ encrypted );
144+
145+ return $ encrypted ;
146+
147+ }
148+
149+ /**
150+ * 私钥解密
151+ * @param $data
152+ * @param string $decrypted
153+ * @return bool|string
154+ */
155+ public function decryptByPrivate ($ data , $ decrypted = '' ){
156+
157+ $ privateKey = $ this ->getPrivateKeyStr ();
158+
159+ if (!$ privateKey ){
160+ return false ;
161+ }
162+
163+ $ data = base64_decode ($ data );
164+
165+ openssl_private_decrypt ($ data , $ decrypted , $ privateKey );
166+
167+ return $ decrypted ;
168+
169+ }
170+
171+ /**
172+ * 通过文件获取key
173+ * @param $filePath
174+ * @return string
175+ */
176+ protected function getKeyStr ($ filePath ){
177+
178+ $ str = file_get_contents ($ filePath );
179+
180+ return $ str ;
181+ }
182+
183+ /**
184+ * 获取private key
185+ * @return string
186+ */
187+ public function getPrivateKeyStr (){
188+
189+ if ($ this ->private_key_str ){
190+ $ key = openssl_pkey_get_private ($ this ->private_key_str );
191+ if ($ key ){
192+ return $ key ;
193+ }else {
194+ $ this ->error = '私钥不可用 ' ;
195+ return false ;
196+ }
197+ }else {
198+ $ this ->error = '私钥不存在 ' ;
199+ return false ;
200+ }
201+
202+ }
203+
204+ /**
205+ * 获取public key
206+ * @return string
207+ */
208+ public function getPublicKeyStr (){
209+
210+ if ($ this ->public_key_str ){
211+ $ key = openssl_pkey_get_public ($ this ->public_key_str );
212+ if ($ key ){
213+ return $ key ;
214+ }else {
215+ $ this ->error = '公钥不可用 ' ;
216+ return false ;
217+ }
218+ }else {
219+ $ this ->error = '公钥不存在 ' ;
220+ return false ;
221+ }
222+
223+ }
224+
225+ public function getError (){
226+
227+ return $ this ->error ;
228+ }
229+
230+
231+ }
0 commit comments