2017-07-19 81 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