Use Laravel’s Crypt without using the framework?

Asked

Viewed 167 times

2

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

https://github.com/illuminate/encryption

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);

He makes a mistake

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

  • Did the answer help you understand? or do you need to explain something?

  • Is giving this Fatal error error: Class 'Illuminate Encryption Encrypter'

  • Take a look now at the editing if you’ve segmented the installation steps! and the editing

1 answer

3


To download the package use the command:

php composer.phar require "illuminate/encryption"

When used outside the 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 that case the body needs a key in the construtor. When used in Laravel this key is removed from the configuration .env which in your case does not have.

Nothing is lost doing so, even it is the correct way in this case and has the same result as if using it in .

Another observation is about the key, because, this construtor can be configured in two ways:

public function __construct($key, 'AES-128-CBC')

that requires a $key 16 in size,

public function __construct($key, 'AES-256-CBC')

that requires a $key 32 in size. The pattern is 'AES-128-CBC', in the example, was not informed, but, the $key has the size of 16 as established.

  • Is giving this Fatal error error: Class 'Illuminate Encryption Encrypter'

  • Put all the code in your question! @Tutijapawada, and anything else the namespace you’re using is wrong! correct is Illuminate Encryption Encrypter($key)

  • 1

    It was settled thanks a lot!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.