
All about .env encryption in Laravel 10
Table of Content
- Encrypt and Decrypt simple
.env
file - Encrypting and Decrypting custom
.env.production
file - Encrypting and Decrypting
.env
using our own key
In a Laravel project, it is not recommended and it is unsafe to send an unencrypted environment file to Git or any other source control. So, in this article, we will encrypt and decrypt the .env
file and discuss everything in detail. If you want to know more, refer to the Laravel Official Documentation.
Encrypt and Decrypt simple .env
file
Suppose we have a fresh Laravel project:

Laravel provides an easy way to encrypt a .env
file using the artisan command.

Now we have encrypted our file and sent it for production.Now Suppose we don't have a .env file.If we try to decrypt it will ask us a key.

So it is very important to save that key , because we need it when we want to decrypt a file.If you have .env file then it will show.

Encrypting and Decrypting custom `.env.production` file
Suppose, you want to hide your main .env file and wanna use .env.production for production,then just copy .env and paste it and rename it .env.production


Now if we want to encrypt .env.production then we need to specify the filename,using 3rd command from options.

You also have to tell the file name, if your exists,it will show you Environment File already exists and if you want to override the file just type --force in the artisan command.

Encrypting and Decrypting `.env` using our own key
You can also specify the key, instead of generating it automatically. If you want to use a custom key.
=Crypt::encryptString(‘your_String’)
It will generate a string, but we only need a 32-character key.
=substr(,0,32)

