laravel/legacy-encrypter
Drop-in Laravel encrypter compatible with legacy apps. Encrypt/decrypt data using older key formats and cipher settings so you can read existing payloads and migrate safely. Useful for upgrades where stored encrypted values must remain accessible.
Installation:
composer require laravel/legacy-encrypter
Add to config/app.php under providers:
Laravel\LegacyEncrypter\LegacyEncrypterServiceProvider::class,
Configuration:
Update config/app.php to use the legacy encrypter in encrypter:
'encrypter' => \Laravel\LegacyEncrypter\LegacyEncrypter::class,
Ensure APP_KEY in .env is set to a 32-character base64-encoded string (legacy mcrypt requirement).
First Use Case: Encrypt/decrypt legacy data during migrations or legacy system integration:
use Laravel\LegacyEncrypter\LegacyEncrypter;
$legacyEncrypter = new LegacyEncrypter(config('app.key'));
$encrypted = $legacyEncrypter->encrypt('sensitive_data');
$decrypted = $legacyEncrypter->decrypt($encrypted);
LegacyEncrypter to decrypt old mcrypt-encrypted data during database migrations.Illuminate\Encryption\Encrypter) for future compatibility.public function up()
{
DB::table('legacy_data')->each(function ($item) {
$legacyEncrypter = new LegacyEncrypter(config('app.key'));
$decrypted = $legacyEncrypter->decrypt($item->encrypted_field);
$newEncrypted = app('encrypter')->encrypt($decrypted);
DB::table('legacy_data')->where('id', $item->id)->update(['encrypted_field' => $newEncrypted]);
});
}
$legacyEncrypter = new LegacyEncrypter(config('app.key'));
$modernEncrypter = app('encrypter');
// Decrypt legacy, encrypt modern
$data = $legacyEncrypter->decrypt($legacyData);
$modernData = $modernEncrypter->encrypt($data);
Route::post('/legacy-api', function (Request $request) {
$legacyEncrypter = new LegacyEncrypter(config('app.key'));
$decrypted = $legacyEncrypter->decrypt($request->encrypted_data);
// Process $decrypted
return response()->json(['data' => $legacyEncrypter->encrypt($responseData)]);
});
$this->app->instance(
\Laravel\LegacyEncrypter\LegacyEncrypter::class,
Mockery::mock(LegacyEncrypter::class)
);
Key Length:
MCRYPT_RIJNDAEL_256).openssl random -base64 32) may not work. Generate a legacy-compatible key:
openssl rand -base64 32 | cut -c1-32
Algorithm Limitations:
rijndael-256 (deprecated in PHP 7.2+). Avoid on modern PHP versions unless necessary.Error Handling:
DecryptException. Catch explicitly:
try {
$legacyEncrypter->decrypt($data);
} catch (\Laravel\LegacyEncrypter\Exceptions\DecryptException $e) {
// Handle corrupt/legacy-incompatible data
}
Performance:
OpenSSL encrypter. Cache decrypted values if reused.Config Override:
Override the encrypter in AppServiceProvider for specific contexts:
$this->app->bind(LegacyEncrypter::class, function () {
return new LegacyEncrypter('custom_legacy_key');
});
Debugging:
\Log::debug('Legacy Encrypt', ['input' => $data, 'output' => $encrypted]);
Extension:
LegacyEncrypter to add custom logic (e.g., key rotation):
class CustomLegacyEncrypter extends LegacyEncrypter {
public function decryptWithFallback($payload) {
try {
return parent::decrypt($payload);
} catch (DecryptException $e) {
return $this->decryptWithOldKey($payload);
}
}
}
Deprecation:
@deprecated and set a sunset date. Use Laravel’s deprecated() helper:
Route::get('/legacy', function () {
deprecated('Legacy endpoint', '2024-12-31');
// ...
});
How can I help you explore Laravel packages today?