欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

laravel 5.3中自定义加密服务的方案详解

程序员文章站 2024-03-12 19:18:44
前言 本文介绍的是laravel 5.3中自定义加密服务的方案,利用laravel的服务容器,实现自定义加密服务注册(示例是支持长字符串的rsa加密),下面来看看详细的介...

前言

本文介绍的是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);
 }
}

在配置中声明

文件地址 /config/app.php 在providershe和aliases中添加

'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中使用的示例

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('rsa_api');
  $crypt->makekey();
  $short = "abcd";
  $long = "
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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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);
 }
}

3、在/routes/web.php添加路由

route::get('/test', 'indexcontroller@test');

4、浏览器访问验证结果

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。