Laravel 5.3 provides a detailed description of the customized encryption service solution, laravel5.3

Source: Internet
Author: User
Tags crypt

Laravel 5.3 provides a detailed description of the customized encryption service solution, laravel5.3

Preface

This article describes the solution for customizing the encryption service in laravel 5.3. Using the service container in laravel, you can register a custom encryption service (in this example, RSA encryption with long strings is supported ), next let's take a look at the detailed introduction:

Create encryption/Decryption Service

The file address/app/Service/Common/CryptService. php code is as follows:

The following is an example of an RSA Encryption Class that supports long strings written by a user. For custom encryption, you only need to change the code of this file. Other operations are only for dependency injection.

<?phpnamespace 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; }}

Create facades

The file address/app/Facades/CryptFacades. php code is as follows:

<?phpnamespace App\Facades;use \Illuminate\Support\Facades\Facade;class CryptFacades extends Facade{ public static function getFacadeAccessor() {  return 'MyCrypt'; }}

Registration Service

The code for creating a file/app/Providers/MyCryptServiceProvider. php is as follows:

In fact, you can also register in AppServiceProvider, so you do not need to create another MyCryptServiceProvider. php file.

In addition, the AppServiceProvider statement is also available in/config/app. php.

<?phpnamespace 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); }}

Declare in configuration

File address/config/app. php added in providershe and aliases

'providers' => [ \App\Providers\MyCryptServiceProvider::class,],'aliases' => [ 'MyCrypt' => \App\Facades\CryptFacades::class,]

Write a configuration file for the custom encryption/Decryption Service

/Config/crypt. php because the CryptService I wrote is useful in the configuration file, I need to add another configuration file. In actual projects, you can set the configuration file and encryption service class as needed.

<? Php // Based on the laravel root directory. The delimiter should be replaced by the DIRECTORY_SEPARATOR constant instead of the 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,];

Example used in Controller

1. artisan creates the Controller File

php artisan make:controller IndexController

2. Edit IndexController

<?phpnamespace 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); }}

3. Add a route in/routes/web. php

Route::get('/test', 'IndexController@test');

4. browser access verification results

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.