Use Laravel's Crypt without using the framework?

2

I need to use the laravel component "Crypt" but separate without using the framework, I installed it via composer.

link

include 'vendor/autoload.php';
use Illuminate\Encryption;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Contracts\Encryption\DecryptException;

$encrypted = Crypt::encryptString('Hello world.');
$decrypted = Crypt::decryptString($encrypted);

It gives an error

  

Fatal error: Uncaught exception 'RuntimeException' with message 'A facade root has not been set.' in /var/www/html/exec/crypt/vendor/illuminate/support/Facades/Facade.php:218 Stack trace: # 0 /var/www/html/exec/crypt/index.php(14): Illuminate \ Support \ Facades \ Facade :: __ callStatic ('encryptString', Array) # 1 /var/www/html/exec/crypt/index.php(14): Illuminate \ Support \ Facades \ Crypt :: encryptString ('Hello world. ') # 2 {main} thrown in /var/www/html/exec/crypt/vendor/illuminate/support/Facades/Facade.php on line 218

    
asked by anonymous 12.01.2018 / 15:42

1 answer

3

To download the package use the command:

  

php composer.phar require "illuminate/encryption"

When used outside framework use your instance as follows:

<?php include 'vendor/autoload.php';

$key = "0123456789123456";
$c = new \Illuminate\Encryption\Encrypter($key);

$r = $c->encryptString('Hello world.');
$s = $c->decryptString($r);

echo $r;
echo PHP_EOL;
echo $s;

Because in this case the instance needs a key in construtor . When used in Laravel this key is removed from the .env setting that in your case does not have.

Do not miss anything doing so, it is even the correct way in this case and has the same result as if you were using it in

12.01.2018 / 20:48