首页  >  文章  >  后端开发  >  讲解如何在laravel中自定义加密服务

讲解如何在laravel中自定义加密服务

*文
*文原创
2018-01-02 17:08:222148浏览

如何在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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn