前言
本文介绍的是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 代码如下:
其实也可以在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('MyCrypt',CryptService::class);
}
}
在配置中声明
PHP的RSA加密解密方法以及开发接口使用本篇文章给大家详细介绍了PHP开发接口使用RSA进行加密解密方法,对此有兴趣的朋友可以学习下。
PHP给源代码加密的几种方法汇总(推荐)下面小编就为大家分享一篇PHP给源代码加密的几种方法汇总(推荐),具有很好的参考价值,希望对大家有所帮助。一起
php实现的AES加密类定义与用法示例这篇文章主要介绍了php实现的AES加密类定义与用法,结合完整实例形式分析了基于php的AES加密类实现及使用方法,需要的
laravel ORM 只开启created_at的几种方法总结下面小编就为大家分享一篇laravel ORM 只开启created_at的几种方法总结,具有很好的参考价值,希望对大家有所帮助。一
通过源码解析Laravel的依赖注入这篇文章主要给大家介绍了如何通过源码解析Laravel的依赖注入的相关资料,文中通过示例代码介绍的非常详细,对大
Laravel中unique和exists验证规则的优化详解这篇文章主要给大家介绍了关于Laravel中unique和exists验证规则的优化的相关资料,文中通过示例代码介绍的非常详细,