From 04f75564752d0daf190b5632caf34200ae2bd875 Mon Sep 17 00:00:00 2001 From: topnuomi <1130395124@qq.com> Date: Wed, 21 Oct 2020 13:20:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=95=BF=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=E5=A4=B1=E8=B4=A5=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/classes/RSA.php | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/classes/RSA.php b/src/classes/RSA.php index 5432854..cac09ae 100644 --- a/src/classes/RSA.php +++ b/src/classes/RSA.php @@ -10,10 +10,20 @@ namespace encrypt\classes; class RSA implements \encrypt\RSA { + /** + * 使用公钥操作 + */ const USE_PUBLIC = 1; + /** + * 使用私钥操作 + */ const USE_PRIVATE = 2; + /** + * 密钥配置 + * @var array + */ private $config = []; /** @@ -64,9 +74,9 @@ class RSA implements \encrypt\RSA * @param int $type * @return null|string */ - public function encrypt(string $data, $type = RSA::USE_PRIVATE): ?string + public function encrypt(string $data, $type = self::USE_PRIVATE): ?string { - if (RSA::USE_PRIVATE === $type) { + if (self::USE_PRIVATE === $type) { $func = 'openssl_private_encrypt'; $key = $this->privateKey(); } else { @@ -74,8 +84,13 @@ class RSA implements \encrypt\RSA $key = $this->publicKey(); } - /** @var string $encrypted */ - return $func($data, $encrypted, $key) ? base64_encode($encrypted) : null; + $string = ''; + foreach (str_split($data, 117) as $chunk) { + /** @var string $encrypted */ + $string .= ($func($chunk, $encrypted, $key) ? base64_encode($encrypted) : null); + } + + return $string; } /** @@ -84,9 +99,9 @@ class RSA implements \encrypt\RSA * @param int $type * @return null|string */ - public function decrypt(string $data, $type = RSA::USE_PRIVATE): ?string + public function decrypt(string $data, $type = self::USE_PRIVATE): ?string { - if (RSA::USE_PRIVATE === $type) { + if (self::USE_PRIVATE === $type) { $func = 'openssl_private_decrypt'; $key = $this->privateKey(); } else { @@ -94,6 +109,12 @@ class RSA implements \encrypt\RSA $key = $this->publicKey(); } - return $func(base64_decode($data), $data, $key) ? $data : null; + $string = ''; + foreach (str_split($data, 172) as $chunk) { + /** @var string $decryptData */ + $string .= ($func(base64_decode($chunk), $decryptData, $key) ? $decryptData : null); + } + + return $string; } }