Search code examples
phpencryptionlaravelmcrypt

Setting the Encryption Key in Laravel results to logout


i need the key that will be used for subsequent Crypt::encrypt() and Crypt::decrypt() calls but only in the current request.

    Crypt::setKey('1');
    $pin_code   = Crypt::encrypt($pin_codes[$key]);

// it do the encryption and then automatically log out

what could be the issue?


Solution

  • I figured it out

    There's only three places Laravel uses the Crypt package.

    1. Caches. Specifically if you're using the Database Cache driver then values placed in the cache are encrypted prior to saving and decrypted when loaded.
    2. Cookies. Cookie values are always sent to the user encrypted. When the request loads, all cookie values are decrypted.
    3. Queues. Specifically if you're using the iron queue driver then values are stored in the queue encrypted and decrypted once retrieved.

    Because of point no 2 ... we will be logged out of the system

    so better to reset key to original

    Crypt::setKey('1'); 
    $pin_code   = Crypt::encrypt($pin_codes[$key]);
    Crypt::setKey(Config::get('app.key'));//reset to original after job is done