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

<?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 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、在/routes/web.php添加路由

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

4、浏览器访问验证结果

总结

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

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/d9be3a078a8bd6942694f0859df0e918.html