grazulex / laravel-configrypt
Encrypt sensitive values in your Laravel .env file and decrypt them automatically at runtime โ safe, seamless, and config-driven.
Requires
- php: ^8.3
- illuminate/console: ^12.0|^13.0
- illuminate/encryption: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
Requires (Dev)
- doctrine/dbal: ^4.2
- larastan/larastan: ^3.4
- laravel/pint: ^1.22
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^3.8|^4.0
- pestphp/pest-plugin-laravel: ^3.2|^4.0
- rector/rector: ^2.0
Suggests
- pestphp/pest: Required to run and generate Configrypt tests (version >=3.0)
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-18 16:13:28 UTC
README
Tip
What Laravel Configrypt does for you โ Keep secrets encrypted inside your .env file so you can commit it, share it and ship it through CI safely. Decrypt them with helpers that keep working even when Laravel caches your config.
This package is free and maintained on my own time. If it saves you hours, a small contribution helps me keep it going: ๐ GitHub Sponsors ยท โ Buy Me a Coffee ยท PayPal
Encrypt sensitive values in your Laravel .env file and decrypt them using helper functions that work around Laravel's environment caching limitations.
๐ Overview
๐ Laravel Configrypt lets you encrypt secrets directly in your .env file using a secure key, and decrypt them using reliable helper functions that work around Laravel's environment caching limitations.
It protects values like API tokens, database credentials, or secret keys โ especially when sharing .env files across environments or storing encrypted configs in source control or CI/CD.
โจ Features
- ๐ Encrypt
.envvalues using AES-256 - ๐ Reliable decryption with helper functions
- ๐ง Seamless Laravel integration via service provider
- ๐ Custom encryption key support (fallback to
APP_KEY) - ๐ก๏ธ Secure by default: decryption only happens inside app runtime
- โ๏ธ Configurable via
config/configrypt.php - ๐งช Safe for CI/CD, secrets rotation, and external vault injection
๐ก Example
In your .env:
MAIL_PASSWORD=ENC:gk9AvRZgx6Jyds7K2uFctw==
In your Laravel code:
// Method 1: Use helper functions (recommended) $password = configrypt_env('MAIL_PASSWORD'); // returns decrypted value $password = encrypted_env('MAIL_PASSWORD'); // alias for configrypt_env() // Method 2: Use the Str macro for easy migration use Illuminate\Support\Str; $password = Str::decryptEnv('MAIL_PASSWORD'); // easy search & replace from env() // Method 3: Use the environment facade use LaravelConfigrypt\Facades\ConfigryptEnv; $password = ConfigryptEnv::get('MAIL_PASSWORD'); // returns decrypted value // Method 4: Manual decryption use LaravelConfigrypt\Facades\Configrypt; $rawValue = env('MAIL_PASSWORD'); // still encrypted due to Laravel's env cache $password = Configrypt::decrypt($rawValue); // manual decrypt // Note: env('MAIL_PASSWORD') returns encrypted value due to Laravel's cache limitation
โ๏ธ Configuration
Publish the config:
php artisan vendor:publish --tag=configrypt-config
Result in config/configrypt.php:
return [ // Use a dedicated key or fallback to APP_KEY 'key' => env('CONFIGRYPT_KEY', env('APP_KEY')), // Prefix used to identify encrypted values 'prefix' => env('CONFIGRYPT_PREFIX', 'ENC:'), // Cipher method 'cipher' => env('CONFIGRYPT_CIPHER', 'AES-256-CBC'), // Auto decrypt (deprecated - has no effect) 'auto_decrypt' => env('CONFIGRYPT_AUTO_DECRYPT', false), ];
๐ Quick Start
1. Install the package
Requirements: PHP 8.3+ and Laravel 12 or 13.
composer require grazulex/laravel-configrypt
2. Publish configuration (optional)
php artisan vendor:publish --tag=configrypt-config
3. Encrypt your secrets
php artisan configrypt:encrypt "my-super-secret-password"
Output:
Encrypted value:
ENC:gk9AvRZgx6Jyds7K2uFctw==
You can now use this encrypted value in your .env file:
SOME_SECRET=ENC:gk9AvRZgx6Jyds7K2uFctw==
4. Add to your .env file
DB_PASSWORD=ENC:gk9AvRZgx6Jyds7K2uFctw== API_SECRET=ENC:XyZ123AbC456DeF789GhI012JkL== JWT_SECRET=ENC:MnOpQrStUvWxYzAbCdEfGhIjKl==
5. Use in your application
โ ๏ธ Important: Laravel's env() function cannot be automatically decrypted due to early caching.
// โ This won't work - Laravel caches env() before our package loads $dbPassword = env('DB_PASSWORD'); // Returns "ENC:xyz..." (still encrypted) // โ Use our helper functions instead (recommended) $dbPassword = configrypt_env('DB_PASSWORD'); // Returns decrypted value $apiSecret = encrypted_env('API_SECRET'); // Alias for consistency // โ Or use the facade for more control use LaravelConfigrypt\Facades\ConfigryptEnv; $dbPassword = ConfigryptEnv::get('DB_PASSWORD'); // โ Or use the Str macro for easy migration use Illuminate\Support\Str; $dbPassword = Str::decryptEnv('DB_PASSWORD');
โ ๏ธ Important: Laravel env() Cache Limitation
Laravel caches environment variables very early in the boot process, before service providers load. This means the standard env() function cannot be automatically decrypted.
๐ง Solution: Use Helper Functions
// โ This won't work - returns encrypted value $password = env('DB_PASSWORD'); // Still returns "ENC:xyz..." // โ These work - return decrypted values $password = configrypt_env('DB_PASSWORD'); $password = encrypted_env('DB_PASSWORD'); $password = ConfigryptEnv::get('DB_PASSWORD');
๐ Quick Migration
Find and replace in your codebase:
# Replace env() calls with configrypt_env() find . -name "*.php" -exec sed -i 's/env(/configrypt_env(/g' {} \; # Or use Str::decryptEnv() for easier reversal find . -name "*.php" -exec sed -i 's/env(/Str::decryptEnv(/g' {} \;
๐ง Advanced Usage
Using the Facades
use LaravelConfigrypt\Facades\Configrypt; use LaravelConfigrypt\Facades\ConfigryptEnv; // Encrypt a value $encrypted = Configrypt::encrypt('my-secret-value'); // Decrypt a value $decrypted = Configrypt::decrypt('ENC:encrypted-value'); // Check if a value is encrypted $isEncrypted = Configrypt::isEncrypted('ENC:some-value'); // Environment-specific methods $dbPassword = ConfigryptEnv::get('DB_PASSWORD'); $allDecrypted = ConfigryptEnv::getAllDecrypted();
Helper Functions
// Primary helper functions (recommended approach) $dbPassword = configrypt_env('DB_PASSWORD', 'default-value'); $apiKey = encrypted_env('API_KEY'); // alias for configrypt_env() // Str macro for easy migration from env() calls use Illuminate\Support\Str; $secret = Str::decryptEnv('JWT_SECRET');
Dependency Injection
use LaravelConfigrypt\Services\ConfigryptService; use LaravelConfigrypt\Services\EnvironmentDecryptor; class MyController extends Controller { public function __construct( private ConfigryptService $configrypt, private EnvironmentDecryptor $envDecryptor ) { } public function encryptValue(Request $request) { $encrypted = $this->configrypt->encrypt($request->value); return response()->json(['encrypted' => $encrypted]); } public function getDecryptedEnv(string $key) { return $this->envDecryptor->get($key); } }
๐งช Practical Examples
Database Configuration
# Encrypt your database password DB_PASSWORD=ENC:W3+f/2ZzZfl9KQ==
// config/database.php 'mysql' => [ 'driver' => 'mysql', 'password' => configrypt_env('DB_PASSWORD'), // Use helper function ],
API Keys Management
# Third-party service credentials STRIPE_SECRET=ENC:Nq8j8hlc3PMp9uE= MAILGUN_SECRET=ENC:XYZ123456789abc= AWS_SECRET_ACCESS_KEY=ENC:AbCdEf1234567890=
// config/services.php 'stripe' => [ 'secret' => configrypt_env('STRIPE_SECRET'), ], 'mailgun' => [ 'secret' => configrypt_env('MAILGUN_SECRET'), ], // config/filesystems.php 's3' => [ 'driver' => 's3', 'secret' => configrypt_env('AWS_SECRET_ACCESS_KEY'), ],
Multi-Environment Setup
# Development CONFIGRYPT_KEY=dev-key-32-characters-long----- DB_PASSWORD=ENC:dev-encrypted-password # Production CONFIGRYPT_KEY=prod-key-32-characters-long---- DB_PASSWORD=ENC:prod-encrypted-password
More examples are available in the Examples Wiki.
๐ Changing Keys
You can define a custom CONFIGRYPT_KEY in .env to use a dedicated encryption key different from APP_KEY.
๐ก Remember: only encrypted values with the correct key can be decrypted. Keep your key safe!
๐ก๏ธ Security Considerations
- Environment Variable Safety: Decrypted values never touch disk after load, only stored in runtime memory
- Prefix Protection:
ENC:prefix ensures only intended values are decrypted - Error Handling: Graceful fallbacks prevent application crashes from decryption failures
- Key Management: Only encrypted values with the correct key can be decrypted - keep your key safe!
- Production Usage: Ideal for
.env.staging,.env.production, or vault-managed.envoverrides - Team Sharing: Perfect for sharing
.envsecurely in teams or across pipelines
๐ Documentation
Comprehensive documentation and examples are available in the GitHub Wiki:
- Installation Guide - Getting started with Laravel Configrypt
- Configuration - Customizing encryption settings
- Basic Usage - Fundamental encryption/decryption operations
- Advanced Usage - Complex scenarios and integrations
- Artisan Commands - Command-line tools reference
- API Reference - Complete API documentation
- Security Considerations - Security best practices
- Troubleshooting - Common issues and solutions
- Examples - Practical usage examples
๐ Support This Package
Laravel Configrypt is free, open source and maintained on my own time. If it saves you hours, here is how you can give back:
- โญ Star the repository โ it helps other developers find it
- ๐ฆ Share it with your team and network
- ๐ Sponsor on GitHub, buy me a coffee or donate via PayPal โ every contribution funds maintenance, new features and Laravel upgrades
๐ License
MIT License โ see LICENSE.md
Made with ๐ for Laravel developers who care about secrets.