0

I'm looking for the way to use a session id for encrypt an ID of file and use it as a temporary url for downloading.

I found just encrypt function in Laravel, but that's not exact what I want. Is there some similar function that could use a session id string for encoding and decoding?

Vaibhavraj Roham
  • 1,138
  • 11
  • 26
fiter
  • 743
  • 1
  • 12
  • 25
  • Here is alternative way http://laravel-recipes.com/recipes/105/encrypting-a-value – Ramzan Mahmood Jul 19 '17 at 12:55
  • Why won't `encrypt` work for you exactly? It will encrypt a string for you which can later be retrieved with decrypt. What else are you looking to do here that `encrypt` won't do? – user1669496 Jul 19 '17 at 13:00
  • You mean I should use Crypt::setKey() ? But it will replace a key in whole app, right? that's can broke something – fiter Jul 19 '17 at 13:01
  • @user3158900 I need to get a url that will be works for user session only. So I have to use session id to encrypt. – fiter Jul 19 '17 at 13:04
  • Please check this link - https://stackoverflow.com/questions/2615554/how-to-encrypt-session-id-in-cookie – Suniti Yadav Sep 06 '17 at 12:50

1 Answers1

1
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