首頁 > 後端開發 > php教程 > 講解如何在laravel自訂加密服務

講解如何在laravel自訂加密服務

*文
發布: 2023-03-19 06:08:02
原創
2187 人瀏覽過

如何在laravel自訂加密服務?本文主要為大家介紹了在laravel 5.3自訂加密服務的相關資料,文中介紹的非常詳細,對大家學習或使用laravel 5.3具有一定的參考學習價值,需要的朋友們下面來一起看看吧。希望對大家有幫助。

前言

本文介紹的是laravel 5.3自訂加密服務的方案,利用laravel的服務容器,實作自訂加密服務註冊(範例是支援長字串的RSA加密),以下來看看詳細的介紹:

#建立加密解密服務類別

檔案位址/app/Service/Common/CryptService.php 程式碼如下

下面這個是個人寫的支援長字串的RSA加密類別作為範例,自訂加密的話只需更改這個檔案的程式碼就好,其它操作只是為了實現依賴注入。

<?php
namespace App\Service\Common;
class CryptService
{
 public $config,$keypath, $prikey_path, $pubkey_path, $prikey, $pubkey , $private_key_size;

 public function select($select = &#39;rsa_api&#39;)
 {
  $config = config(&#39;crypt&#39;);
  if (array_key_exists($select, $config)) {
   $this->config = $config[$select];
   $this->private_key_size = $this->config[&#39;openssl_config&#39;][&#39;private_key_bits&#39;];
  } else {
   return false;
  }
  $this->keypath = dirname(dirname(dirname(__DIR__))) . $this->config[&#39;path&#39;];
  if(!file_exists($this->keypath)){
   mkdir($this->keypath,"0777",true);
  }
  $this->prikey_path = $this->keypath . $this->config[&#39;private_key_file_name&#39;];
  $this->pubkey_path = $this->keypath . $this->config[&#39;public_key_file_name&#39;];
  if (file_exists($this->prikey_path))
   $this->prikey = file_get_contents($this->prikey_path);
  if (file_exists($this->pubkey_path))
   $this->pubkey = file_get_contents($this->pubkey_path);
  return $this;
 }

 public function makeKey()
 {
  $res = openssl_pkey_new($this->config[&#39;openssl_config&#39;]);
  openssl_pkey_export($res, $this->prikey);
  file_put_contents($this->prikey_path, $this->prikey);
  $pubkey = openssl_pkey_get_details($res);
  $this->pubkey = $pubkey[&#39;key&#39;];
  file_put_contents($this->pubkey_path, $this->pubkey);
  return $test = [&#39;prikey&#39; => $this->prikey, &#39;pubkey&#39; => $this->pubkey];
 }

 public function encryptPrivate($data){
  $crypt = $this->encrypt_split($data);
  $crypted = &#39;&#39;;
  foreach ($crypt as $k=>$c){
   if($k!=0) $crypted.="@";
   $crypted.=base64_encode($this->doEncryptPrivate($c));
  }
  return $crypted;
 }
 public function encryptPublic($data){
  $crypt = $this->encrypt_split($data);
  $crypted = &#39;&#39;;
  foreach ($crypt as $k=>$c){
   if($k!=0) $crypted.="@";
   $crypted.=base64_encode($this->doEncryptPublic($c));
  }
  return $crypted;
 }

 public function decryptPublic($data){
  $decrypt = explode(&#39;@&#39;,$data);
  $decrypted = "";
  foreach ($decrypt as $k=>$d){
   $decrypted .= $this->doDecryptPublic(base64_decode($d));
  }
  return $decrypted;
 }
 public function decryptPrivate($data){
  $decrypt = explode(&#39;@&#39;,$data);
  $decrypted = "";
  foreach ($decrypt as $k=>$d){
   $decrypted .= $this->doDecryptPrivate(base64_decode($d));
  }
  return $decrypted;
 }
 private function encrypt_split($data){
  $crypt=[];$index=0;
  for($i=0; $i<strlen($data); $i+=117){
   $src = substr($data, $i, 117);
   $crypt[$index] = $src;
   $index++;
  }
  return $crypt;
 }
 private function doEncryptPrivate($data)
 {
  $rs = &#39;&#39;;
  if (@openssl_private_encrypt($data, $rs, $this->prikey) === FALSE) {
   return NULL;
  }
  return $rs;
 }

 private function doDecryptPrivate($data)
 {
  $rs = &#39;&#39;;
  if (@openssl_private_decrypt($data, $rs, $this->prikey) === FALSE) {
   return null;
  }
  return $rs;
 }
 private function doEncryptPublic($data){
  $rs = &#39;&#39;;
  if (@openssl_public_encrypt($data, $rs, $this->pubkey) === FALSE) {
   return NULL;
  }
  return $rs;
 }
 private function doDecryptPublic($data)
 {
  $rs = &#39;&#39;;
  if (@openssl_public_decrypt($data, $rs, $this->pubkey) === FALSE) {
   return null;
  }
  return $rs;
 }
}
登入後複製

建立門面facades

檔案位址/app/Facades/CryptFacades.php 程式碼如下:

<?php
namespace App\Facades;
use \Illuminate\Support\Facades\Facade;

class CryptFacades extends Facade{
 public static function getFacadeAccessor()
 {
  return &#39;MyCrypt&#39;;
 }
}
登入後複製

註冊服務

建立檔案/app/Providers/MyCryptServiceProvider.php 程式碼如下:

##其實也可以在AppServiceProvider中註冊,就不用另外建個MyCryptServiceProvider.php檔案了


而且在/config/app.php中一般也已經有了AppServiceProvider的宣告

#

<?php
namespace App\Providers;

use App\Service\Common\CryptService;
use Illuminate\Support\ServiceProvider;

class MyCryptServiceProvider extends ServiceProvider
{
 /**
  * Bootstrap the application services.
  *
  * @return void
  */
 public function boot()
 {
  //
 }

 /**
  * Register the application services.
  *
  * @return void
  */
 public function register()
 {
  \App::bind(&#39;MyCrypt&#39;,CryptService::class);
 }
}
登入後複製

#在設定中宣告

檔案位址/config/app.php 在providershe和aliases中加入

&#39;providers&#39; => [
 \App\Providers\MyCryptServiceProvider::class,
],

&#39;aliases&#39; => [
 &#39;MyCrypt&#39; => \App\Facades\CryptFacades::class,
]
登入後複製

##編寫自訂加密解密服務的設定檔
/config/crypt.php 因為我寫的CryptService有用到設定文件,所以需要再新增個設定檔。在實際專案中,可以根據需要自行設定設定檔和加密服務類別。

<?php
//基于laravel根目录,分隔符最好是用 DIRECTORY_SEPARATOR 常量代替
return [
 &#39;rsa_api&#39; => [
  &#39;path&#39;=>DIRECTORY_SEPARATOR.&#39;storage&#39;.DIRECTORY_SEPARATOR.&#39;rsakey&#39;.DIRECTORY_SEPARATOR,
  &#39;private_key_file_name&#39;=>&#39;private_key.pem&#39;,
  &#39;public_key_file_name&#39; =>&#39;public_key.pem&#39;,
  &#39;openssl_config&#39;=>[
   "digest_alg" => "sha512",
   "private_key_bits" => 1024,
   "private_key_type" => OPENSSL_KEYTYPE_RSA,
  ]
 ],
 &#39;rsa_data&#39;=>[
  &#39;path&#39;=>DIRECTORY_SEPARATOR.&#39;storage&#39;.DIRECTORY_SEPARATOR.&#39;rsakey&#39;.DIRECTORY_SEPARATOR,
  &#39;private_key_file_name&#39;=>&#39;private.pem&#39;,
  &#39;public_key_file_name&#39; =>&#39;public.pem&#39;,
  &#39;openssl_config&#39;=>[
   "digest_alg" => "sha512",
   "private_key_bits" => 1024,
   "private_key_type" => OPENSSL_KEYTYPE_RSA,
  ]
 ]
];
登入後複製

在Controller中使用的範例

1、artisan建立Controller檔案

php artisan make:controller IndexController
登入後複製

2、編輯IndexController

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use MyCrypt;
class IndexController extends Controller{

 public function test(){
  $crypt = MyCrypt::select(&#39;rsa_api&#39;);
  $crypt->makeKey();
  $short = "abcd";
  $long = "
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

  $req[&#39;short&#39;] = $short;
  $req[&#39;short_private_encrypt&#39;] = $crypt->encryptPrivate($short);
  $req[&#39;short_public_decrypt&#39;] = $crypt->decryptPublic($req[&#39;short_private_encrypt&#39;]);

  $req[&#39;long&#39;] = $long;
  $req[&#39;long_private_encrypt&#39;] = $crypt->encryptPrivate($long);
  $req[&#39;long_public_decrypt&#39;] = $crypt->decryptPublic($req[&#39;long_private_encrypt&#39;]);
  dump($req);
  //dd($req);
 }
}
登入後複製

3、在/routes/web.php新增路由

Route::get(&#39;/test&#39;, &#39;IndexController@test&#39;);
登入後複製
4、瀏覽器存取驗證結果

相關推薦:

#探究Laravel的中間件是如何實現的

laravel編寫APP介面(API)

#提升Laravel 5效能的一些實用技巧

以上是講解如何在laravel自訂加密服務的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板