6
6
7
7
class RSACrypt
8
8
{
9
- public $ config = [
10
- 'digest_alg ' => 'sha512 ' ,
11
- 'private_key_bits ' => 4096 ,
9
+ public $ config = [
10
+ 'digest_alg ' => 'sha256 ' ,
11
+ 'private_key_bits ' => 2048 ,
12
12
'private_key_type ' => OPENSSL_KEYTYPE_RSA ,
13
13
];
14
- private $ private_key ;
15
- private $ public_key ;
16
- private $ max_length ;
14
+ private $ private_key = '' ;
15
+ private $ public_key = '' ;
16
+ private $ encrypt_block_size = 200 ;
17
+ private $ decrypt_block_size = 256 ;
17
18
18
- public function __construct ($ max_length = 117 )
19
+ public function __construct ($ config = [] )
19
20
{
20
- $ this ->max_length = $ max_length ;
21
+ foreach ($ config as $ key => $ value ) {
22
+ if (isset ($ this ->{$ key })) {
23
+ $ this ->{$ key } = $ value ;
24
+ }
25
+ }
21
26
}
22
27
23
28
/**
@@ -55,11 +60,14 @@ public function publicKey($public_key = null)
55
60
/**
56
61
* create a pair of private&public key.
57
62
*
63
+ * @param array $config
64
+ *
58
65
* @return $this
59
66
*/
60
- public function create (): self
67
+ public function create (array $ config = [] ): self
61
68
{
62
- $ res = openssl_pkey_new ($ this ->config );
69
+ $ config = array_merge ($ this ->config , $ config );
70
+ $ res = openssl_pkey_new ($ config );
63
71
openssl_pkey_export ($ res , $ pri_key );
64
72
$ this ->privateKey ($ pri_key );
65
73
$ res = openssl_pkey_get_details ($ res );
@@ -68,35 +76,21 @@ public function create(): self
68
76
return $ this ;
69
77
}
70
78
71
- /**
72
- * @param $data
73
- *
74
- * @throws \ErrorException
75
- */
76
79
public function encryptByPrivateKey (string $ data ): string
77
80
{
78
81
return $ this ->encrypt ($ data , 'private ' );
79
82
}
80
83
81
- /**
82
- * @throws \ErrorException
83
- */
84
84
public function encryptByPublicKey (string $ data ): string
85
85
{
86
86
return $ this ->encrypt ($ data , 'public ' );
87
87
}
88
88
89
- /**
90
- * @throws \ErrorException
91
- */
92
89
public function decryptByPrivateKey (string $ data ): string
93
90
{
94
91
return $ this ->decrypt ($ data , 'private ' );
95
92
}
96
93
97
- /**
98
- * @throws \ErrorException
99
- */
100
94
public function decryptByPublicKey (string $ data ): string
101
95
{
102
96
return $ this ->decrypt ($ data , 'public ' );
@@ -106,64 +100,39 @@ public function decryptByPublicKey(string $data): string
106
100
* @param $data
107
101
* @param string $type
108
102
*
109
- * @throws \ErrorException
110
- *
111
103
* @return string
112
104
*/
113
105
private function encrypt ($ data , $ type = 'private ' )
114
106
{
115
- $ str = '' ;
116
- $ count = 0 ;
117
- for ($ i = 0 ; $ i < \strlen ($ data ); $ i += $ this ->max_length ) {
118
- $ src = substr ($ data , $ i , 117 );
119
- $ result = 'private ' === $ type ?
120
- @openssl_private_encrypt ($ src , $ out , $ this ->private_key ) :
121
- @openssl_public_encrypt ($ src , $ out , $ this ->public_key );
122
- if (false === $ result ) {
123
- throw new \ErrorException ('Failed encrypt by ' . $ type . ' key. string : ' . $ src );
124
- }
125
- $ str .= 0 == $ count ? base64_encode ($ out ) : ', ' . base64_encode ($ out );
126
- ++$ count ;
107
+ $ str = '' ;
108
+ $ data = str_split ($ data , $ this ->encrypt_block_size );
109
+ foreach ($ data as $ chunk ) {
110
+ $ partial = '' ;
111
+ 'private ' === $ type ?
112
+ @openssl_private_encrypt ($ chunk , $ partial , $ this ->private_key ) :
113
+ @openssl_public_encrypt ($ chunk , $ partial , $ this ->public_key );
114
+ $ str .= $ partial ;
127
115
}
128
116
129
- return $ str ;
117
+ return base64_encode ( $ str) ;
130
118
}
131
119
132
120
/**
133
121
* @param $data
134
122
* @param string $type
135
123
*
136
- * @throws \ErrorException
137
- *
138
124
* @return string
139
125
*/
140
- private function decrypt ($ data , $ type = ' private ' )
126
+ private function decrypt ($ data , $ type )
141
127
{
142
- $ str = '' ;
143
- if (strpos ($ data , ', ' )) {
144
- $ dataArray = explode (', ' , $ data );
145
- foreach ($ dataArray as $ src ) {
146
- $ result = 'private ' === $ type ?
147
- @openssl_private_encrypt (base64_decode ($ src ), $ out , $ this ->private_key ) :
148
- @openssl_public_decrypt (base64_decode ($ src ), $ out , $ this ->public_key );
149
- if (false === $ result ) {
150
- throw new \ErrorException ('Failed decrypt by ' . $ type . ' key. string : ' . $ src );
151
- }
152
- $ str .= $ out ;
153
- }
154
- } else {
155
- dump ('private ' === $ type );
156
-
157
- $ out = '' ;
158
- $ src = base64_decode ($ data );
159
- $ result = 'private ' === $ type ?
160
- @openssl_private_encrypt ($ src , $ out , $ this ->private_key ) :
161
- @openssl_public_decrypt ($ src , $ out , $ this ->public_key );
162
- if (false === $ result ) {
163
- throw new \ErrorException ('Failed decrypt by ' . $ type . ' key. string : ' . $ data );
164
- }
165
- die ();
166
- $ str = $ out ;
128
+ $ str = '' ;
129
+ $ data = str_split (base64_decode ($ data ), $ this ->decrypt_block_size );
130
+ foreach ($ data as $ chunk ) {
131
+ $ partial = '' ;
132
+ 'private ' === $ type ?
133
+ openssl_private_decrypt ($ chunk , $ partial , $ this ->private_key , OPENSSL_PKCS1_PADDING ) :
134
+ openssl_public_decrypt ($ chunk , $ partial , $ this ->public_key , OPENSSL_PKCS1_PADDING );
135
+ $ str .= $ partial ;
167
136
}
168
137
169
138
return $ str ;
0 commit comments