From 505a892352b5e35eb2030370ebb0267ec06a7a69 Mon Sep 17 00:00:00 2001 From: zhang zhuo Date: Sat, 10 Jan 2026 16:15:06 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=8A=A0=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/Admin/Login.php | 4 ++++ app/Utils/Str.php | 25 +++++++++++++++++++++++++ config/autoload/app.php | 6 +++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/Controller/Admin/Login.php b/app/Controller/Admin/Login.php index 5015eb1..bace9fd 100644 --- a/app/Controller/Admin/Login.php +++ b/app/Controller/Admin/Login.php @@ -49,6 +49,10 @@ class Login extends Base $param = Param::only(['username', 'password', 'uuid', 'code']); $request = $this->container->get(aRequest::class); $request->scene('login')->validateResolved(); + // 数据解密 + $param['username'] = Str::RsaDecrypt($param['username']); + $param['password'] = Str::RsaDecrypt($param['password']); + $param['code'] = Str::RsaDecrypt($param['code']); // 验证码 $code = $this->cache->get("VER:" . $param['uuid']); if (!$code) { diff --git a/app/Utils/Str.php b/app/Utils/Str.php index 24d0c33..204fff2 100644 --- a/app/Utils/Str.php +++ b/app/Utils/Str.php @@ -5,6 +5,8 @@ namespace App\Utils; +use App\Exception\ServiceException; +use function Hyperf\Config\config; use function PHPUnit\Framework\matches; class Str @@ -161,4 +163,27 @@ class Str return [$first, $rest]; } + static function RsaEncrypt(string $data): string + { + $publicKey = "-----BEGIN PUBLIC KEY-----\n" . wordwrap(config('app.rsa_public_key'), 64, "\n", true) . "\n-----END PUBLIC KEY-----"; + $ok = openssl_public_encrypt($data, $encrypted, $publicKey, OPENSSL_PKCS1_PADDING); + if (!$ok) { + throw new ServiceException('RSA encrypt failed'); + } + return base64_encode($encrypted); + } + + static function RsaDecrypt(string $data): string + { + $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" . wordwrap(config('app.rsa_private_key'), 64, "\n", true) . "\n-----END RSA PRIVATE KEY-----"; + $cipher = base64_decode($data); + if ($cipher === false) { + throw new ServiceException('Invalid base64 data'); + } + $ok = openssl_private_decrypt($cipher, $plain, $privateKey, OPENSSL_PKCS1_PADDING); + if (!$ok) { + throw new ServiceException('RSA decrypt failed'); + } + return $plain; + } } \ No newline at end of file diff --git a/config/autoload/app.php b/config/autoload/app.php index 6059798..a25f841 100644 --- a/config/autoload/app.php +++ b/config/autoload/app.php @@ -7,5 +7,9 @@ use function Hyperf\Support\env; */ return [ // 域名 - 'domain' => env('DOMAIN') + 'domain' => env('DOMAIN'), + // rsa公钥 + 'rsa_public_key' => env('RSA_PUBLIC_KEY'), + // rsa私钥 + 'rsa_private_key' => env('RSA_PRIVATE_KEY'), ]; \ No newline at end of file