如何在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 = 'rsa_api') { $config = config('crypt'); if (array_key_exists($select, $config)) { $this->config = $config[$select]; $this->private_key_size = $this->config['openssl_config']['private_key_bits']; } else { return false; } $this->keypath = dirname(dirname(dirname(__DIR__))) . $this->config['path']; if(!file_exists($this->keypath)){ mkdir($this->keypath,"0777",true); } $this->prikey_path = $this->keypath . $this->config['private_key_file_name']; $this->pubkey_path = $this->keypath . $this->config['public_key_file_name']; 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['openssl_config']); openssl_pkey_export($res, $this->prikey); file_put_contents($this->prikey_path, $this->prikey); $pubkey = openssl_pkey_get_details($res); $this->pubkey = $pubkey['key']; file_put_contents($this->pubkey_path, $this->pubkey); return $test = ['prikey' => $this->prikey, 'pubkey' => $this->pubkey]; } public function encryptPrivate($data){ $crypt = $this->encrypt_split($data); $crypted = ''; 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 = ''; foreach ($crypt as $k=>$c){ if($k!=0) $crypted.="@"; $crypted.=base64_encode($this->doEncryptPublic($c)); } return $crypted; } public function decryptPublic($data){ $decrypt = explode('@',$data); $decrypted = ""; foreach ($decrypt as $k=>$d){ $decrypted .= $this->doDecryptPublic(base64_decode($d)); } return $decrypted; } public function decryptPrivate($data){ $decrypt = explode('@',$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 = ''; if (@openssl_private_encrypt($data, $rs, $this->prikey) === FALSE) { return NULL; } return $rs; } private function doDecryptPrivate($data) { $rs = ''; if (@openssl_private_decrypt($data, $rs, $this->prikey) === FALSE) { return null; } return $rs; } private function doEncryptPublic($data){ $rs = ''; if (@openssl_public_encrypt($data, $rs, $this->pubkey) === FALSE) { return NULL; } return $rs; } private function doDecryptPublic($data) { $rs = ''; 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 'MyCrypt'; } }
註冊服務
建立檔案/app/Providers/MyCryptServiceProvider.php 程式碼如下:
#
<?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('MyCrypt',CryptService::class); } }
#在設定中宣告
'providers' => [ \App\Providers\MyCryptServiceProvider::class, ], 'aliases' => [ 'MyCrypt' => \App\Facades\CryptFacades::class, ]
##編寫自訂加密解密服務的設定檔
/config/crypt.php 因為我寫的CryptService有用到設定文件,所以需要再新增個設定檔。在實際專案中,可以根據需要自行設定設定檔和加密服務類別。
<?php //基于laravel根目录,分隔符最好是用 DIRECTORY_SEPARATOR 常量代替 return [ 'rsa_api' => [ 'path'=>DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'rsakey'.DIRECTORY_SEPARATOR, 'private_key_file_name'=>'private_key.pem', 'public_key_file_name' =>'public_key.pem', 'openssl_config'=>[ "digest_alg" => "sha512", "private_key_bits" => 1024, "private_key_type" => OPENSSL_KEYTYPE_RSA, ] ], 'rsa_data'=>[ 'path'=>DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'rsakey'.DIRECTORY_SEPARATOR, 'private_key_file_name'=>'private.pem', 'public_key_file_name' =>'public.pem', 'openssl_config'=>[ "digest_alg" => "sha512", "private_key_bits" => 1024, "private_key_type" => OPENSSL_KEYTYPE_RSA, ] ] ];
在Controller中使用的範例
php artisan make:controller IndexController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use MyCrypt;
class IndexController extends Controller{
public function test(){
$crypt = MyCrypt::select('rsa_api');
$crypt->makeKey();
$short = "abcd";
$long = "
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
$req['short'] = $short;
$req['short_private_encrypt'] = $crypt->encryptPrivate($short);
$req['short_public_decrypt'] = $crypt->decryptPublic($req['short_private_encrypt']);
$req['long'] = $long;
$req['long_private_encrypt'] = $crypt->encryptPrivate($long);
$req['long_public_decrypt'] = $crypt->decryptPublic($req['long_private_encrypt']);
dump($req);
//dd($req);
}
}
Route::get('/test', 'IndexController@test');
相關推薦:
以上是講解如何在laravel自訂加密服務的詳細內容。更多資訊請關注PHP中文網其他相關文章!