Skip to content

Commit 7355b85

Browse files
committed
patch RSACrypt
1 parent 0269aee commit 7355b85

File tree

1 file changed

+36
-67
lines changed

1 file changed

+36
-67
lines changed

src/RSACrypt.php

Lines changed: 36 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,23 @@
66

77
class RSACrypt
88
{
9-
public $config = [
10-
'digest_alg' => 'sha512',
11-
'private_key_bits' => 4096,
9+
public $config = [
10+
'digest_alg' => 'sha256',
11+
'private_key_bits' => 2048,
1212
'private_key_type' => OPENSSL_KEYTYPE_RSA,
1313
];
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;
1718

18-
public function __construct($max_length = 117)
19+
public function __construct($config = [])
1920
{
20-
$this->max_length = $max_length;
21+
foreach ($config as $key => $value) {
22+
if (isset($this->{$key})) {
23+
$this->{$key} = $value;
24+
}
25+
}
2126
}
2227

2328
/**
@@ -55,11 +60,14 @@ public function publicKey($public_key = null)
5560
/**
5661
* create a pair of private&public key.
5762
*
63+
* @param array $config
64+
*
5865
* @return $this
5966
*/
60-
public function create(): self
67+
public function create(array $config = []): self
6168
{
62-
$res = openssl_pkey_new($this->config);
69+
$config = array_merge($this->config, $config);
70+
$res = openssl_pkey_new($config);
6371
openssl_pkey_export($res, $pri_key);
6472
$this->privateKey($pri_key);
6573
$res = openssl_pkey_get_details($res);
@@ -68,35 +76,21 @@ public function create(): self
6876
return $this;
6977
}
7078

71-
/**
72-
* @param $data
73-
*
74-
* @throws \ErrorException
75-
*/
7679
public function encryptByPrivateKey(string $data): string
7780
{
7881
return $this->encrypt($data, 'private');
7982
}
8083

81-
/**
82-
* @throws \ErrorException
83-
*/
8484
public function encryptByPublicKey(string $data): string
8585
{
8686
return $this->encrypt($data, 'public');
8787
}
8888

89-
/**
90-
* @throws \ErrorException
91-
*/
9289
public function decryptByPrivateKey(string $data): string
9390
{
9491
return $this->decrypt($data, 'private');
9592
}
9693

97-
/**
98-
* @throws \ErrorException
99-
*/
10094
public function decryptByPublicKey(string $data): string
10195
{
10296
return $this->decrypt($data, 'public');
@@ -106,64 +100,39 @@ public function decryptByPublicKey(string $data): string
106100
* @param $data
107101
* @param string $type
108102
*
109-
* @throws \ErrorException
110-
*
111103
* @return string
112104
*/
113105
private function encrypt($data, $type = 'private')
114106
{
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;
127115
}
128116

129-
return $str;
117+
return base64_encode($str);
130118
}
131119

132120
/**
133121
* @param $data
134122
* @param string $type
135123
*
136-
* @throws \ErrorException
137-
*
138124
* @return string
139125
*/
140-
private function decrypt($data, $type = 'private')
126+
private function decrypt($data, $type)
141127
{
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;
167136
}
168137

169138
return $str;

0 commit comments

Comments
 (0)