2017-07-19 35 views
0

私は、セッションIDを使用してファイルのIDを暗号化し、ダウンロード用の一時URLとして使用する方法を探しています。Laravel 5.4セッションIDによる暗号化

私はちょうどencryptの機能をLaravelで見つけましたが、それは私が欲しいものではありません。エンコードとデコードにセッションID文字列を使用できる同様の機能はありますか?

+0

ここはまさにあなたのためhttp://laravel-recipes.com/recipes/105/encrypting-a-value –

+0

ないのはなぜ '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 
関連する問題