Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DaveBagler's avatar

Using the Crypt facade in the config files.

I'm working on an artisan command for appending encrypted values to the .env file. This works fine for any variable I can process in the app before using it (app keys, oauth tokens, etc...) because I can decrypt the values.

However when I try to use the Crypt facade in the config file I get an error. For example if I try to wrap my database connection variables in a call to Crypt::decrypt() call I get a fatal error. The message back is that the instance is null.

My guess is that the application object isn't ready, but I'm not sure. Any advice?

Hopefully if I can get this working I can tidy up the code of the artisan command and release it as a package.

0 likes
15 replies
DaveBagler's avatar

at the top of the file:

use Illuminate\Support\Facades\Crypt;

in the return array:

...
        'mysql' => [
            'driver'    => 'mysql',
            'host'      => Crypt::decrypt(env('DB_HOST', 'localhost')),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'port'      => env('DB_PORT', '3306'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],
...
bobbybouwmann's avatar

Ok one question first, why do you even encrypt it? I mean if you have a .env file you don't have to encrypt it!

DaveBagler's avatar

Intuit requires that app keys and OAuth tokens be encrypted even if they are stored in a .env file.

Also being able to encrypt database connection information would be a bonus, but given the issue I'm running into it may not be be possible.

bobbybouwmann's avatar
Level 88

I'm not sure about this, but have you tried to use the full namespace to the facade or to the real class?

// Facade
Illuminate\Support\Facades\Crypt::decrypt($value);

// Class
new Illuminate\Encryption\Encrypter()->decrypt($value):
DaveBagler's avatar

Thanks for the suggestions, I tried both, but it looks like the Application object is not usable at that stage in the bootstrap.

DaveBagler's avatar

Sorry, my mistake the the second one works.

I need to create the object first outside of the call so at the top:

$crypt = new Illuminate\Encryption\Encrypter(env('APP_KEY'));

And then in the connection array:

'host'      => $crypt->decrypt(env('DB_HOST')),
bobbybouwmann's avatar

Yea awesome ;) That would have been my next suggestion :P Glad you fixed it :)

DaveBagler's avatar

Yeah, now just to package it up for release, thanks for very prompt (and helpful) help!

rezasys's avatar

Hello I have a problem with this solution :

The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.

My laravel version is 5.5.

CaptAm's avatar

This worked for me in Laravel 8.x.

In very beginning of the file:

$crypt = new Illuminate\Encryption\Encrypter(base64_decode(Str::after(env('APP_KEY'), 'base64:')), 'AES-256-CBC');

and then in array element:

'username' => $crypt->decrypt(env('USER_NAME')),

Hope this helps ;-)

justinhartman's avatar

@CaptAm Your solution almost worked perfectly in Laravel 9x but I had to import the Str facade for this to work. So, my config file looks like this now:

use Illuminate\Support\Str;

$crypt = new Illuminate\Encryption\Encrypter(
    base64_decode(Str::after(env('APP_KEY'), 'base64:')),
    'aes-256-cbc'
);

return [
    'password' => $crypt->decryptString('xxxxx'),
];
keyesr's avatar

This is not working for me in Laravel 8.x., error coming

The MAC is invalid.

Please or to participate in this conversation.