2017-07-19 54 views
0

我正在寻找使用会话ID加密文件ID的方式,并将其用作下载的临时URL。Laravel 5.4按会话ID加密

我在Laravel中发现了encrypt函数,但这并不是我想要的。是否有一些类似的功能可以使用会话ID字符串进行编码和解码?

+0

这里是另一种方式http://laravel-recipes.com/recipes/105/encrypting-a-value –

+0

为什么不'你encrypt'工作是什么呢?它会为您加密一个字符串,稍后可以通过解密进行检索。你还想在这里做什么'encrypt'不会做? – user3158900

+0

你的意思是我应该使用Crypt :: setKey()?但它会取代整个应用程序的关键,对吧?这可以打破东西 – fiter

回答

0
make function in 
**Path - /app/Libraries/Scramble.php** 

**<Scramble.php>** 
<?php 

namespace App\Libraries; 

use Crypt; 
use Session; 
use Illuminate\Contracts\Encryption\EncryptException; 

class Scramble 
{ 

    public function __construct() 
    { 
    } 

    /** 
    * Encrypt the given value with session binding. 
    * 
    * @param string $value 
    * 
    * @return string 
    * 
    * @throws \Illuminate\Contracts\Encryption\EncryptException 
    */ 
    public static function encrypt($value) 
    { 
     if ($value === false) { 
      throw new EncryptException('Could not encrypt the data.'); 
     } 
     $manupulate_val = Session::getId()."##".config('app.key')."##".$value; 
     return Crypt::encrypt($manupulate_val); 
    } 

    /** 
    * Decrypt the given value. 
    * 
    * @param string $decrypted 
    * @return string 
    * 
    * @throws \Illuminate\Contracts\Encryption\DecryptException 
    */ 
    public static function decrypt($decrypted) 
    { 
     if ($decrypted === false) { 
      throw new DecryptException('Could not decrypt the data.'); 
     } 

     $sess_id   = Session::getId(); 
     $decryptedStr = Crypt::decrypt($decrypted); 
     $decryptedStrArr = explode("##", $decryptedStr); 

     if (is_array($decryptedStrArr) && $decryptedStrArr['0'] !== $sess_id) { 
      abort(400); 
     } 

     if (is_array($decryptedStrArr) && $decryptedStrArr['1'] !== config('app.key')) { 
      abort(400); 
     } 

     return $decryptedStrArr['2']; 
    } 
} 
**</scramble.php>** 

now you can call anywhere.... 

use App\Libraries\Scramble; 

$Yourid = 12345; 
$sessionIdWithencData = Scramble::encrypt($Yourid); 
$sessionIdWithdecData = Scramble::decrypt($sessionIdWithencData); 
dd($sessionIdWithdecData); 
========================================== 


i hope this is help full