-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProvider
76 lines (75 loc) · 2.29 KB
/
Provider
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
<?php
declare(strict_types = 1);
namespace vendor\rsa;
class Provider
{
private $_config;
public function __construct(array $config)
{
$this->_config = $config;
}
/**
* 私钥加密
* @param string $data 要加密的数据
* @return string 加密后的字符串
*/
public function privateKeyEncode(string $data) : string
{
$this->_needKey('private_key');
$private_key = openssl_pkey_get_private($this->_config['private_key']);
$data = base64_encode($data);
openssl_private_encrypt($data, $encrypted, $private_key);
return !empty($encrypted)?base64_encode($encrypted):'';
}
/**
* 公钥加密
* @param string $data 要加密的数据
* @return string 加密后的字符串
*/
public function publicKeyEncode(string $data) : string
{
$this->_needKey('public_key');
$public_key = openssl_pkey_get_public($this->_config['public_key']);
$data = base64_encode($data);
openssl_public_encrypt($data, $encrypted, $public_key);
return !empty($encrypted)?base64_encode($encrypted):'';
}
/**
* 用公钥解密私钥加密内容
* @param string $data 要解密的数据
* @return string 解密后的字符串
*/
public function decodePrivateEncode(string $data) : string
{
$this->_needKey('public_key');
$public_key = openssl_pkey_get_public($this->_config['public_key']);
openssl_public_decrypt(base64_decode($data), $decrypted, $public_key);
return !empty($decrypted)?base64_decode($decrypted):''; //把拼接的数据base64_decode 解密还原
}
/**
* 用私钥解密公钥加密内容
* @param string $data 要解密的数据
* @return string 解密后的字符串
*/
public function decodePublicEncode(string $data) : string
{
$this->_hasKey('private_key');
$private_key = openssl_pkey_get_private($this->_config['private_key']);
$data = base64_decode($data);
openssl_private_decrypt($data, $decrypted, $private_key); //私钥解密
return !empty($decrypted)?base64_decode($decrypted):'';
}
/**
* 检查是否 含有所需配置文件
* @param string $key public_key 公钥 private_key 私钥
* @return bool
* @throws \Exception
*/
private function _hasKey(string $key = 'public_key') : bool
{
if(!isset($this->_config[$key])) {
throw new \Exception('请配置密匙');
}
return true;
}
}