Ошибка шифрования в Laravel 10
Начал делать логику шифрования/дешифрования, нашел такой пример:
$newEncrypter = new \Illuminate\Encryption\Encrypter($key, config('app.cipher'));
$encrypted = $newEncrypter->encrypt($plainTextToEncrypt);
$decrypted = $newEncrypter->decrypt($encrypted);
Завел тинкер и получаю следующую ошибку, куда копать?
Psy Shell v0.12.0 (PHP 8.2.8 — cli) by Justin Hileman
> config('app.key')
= "base64:TmT9pX6lRfDcWrFWlyLSYSOnyRShEiEFCv1MOD5aT+A="
> config('app.cipher')
= "AES-256-CBC"
> $newEncrypter = new \Illuminate\Encryption\Encrypter(config('app.key'), config('app.cipher'));
RuntimeException Unsupported cipher or incorrect key length. Supported ciphers are: aes-128-cbc, aes-256-cbc, aes-128-gcm, aes-256-gcm.
Чуть больше о приложении:
Environment ......................................................................................................................................
Application Name ................................................................................................. ****** ******* ******** (local)
Laravel Version .......................................................................................................................... 10.43.0
PHP Version ................................................................................................................................ 8.2.8
Composer Version ........................................................................................................................... 2.6.6
Environment ................................................................................................................................ local
Debug Mode ............................................................................................................................... ENABLED
URL ............................................................................................................................... 127.0.0.1:8000
Maintenance Mode ............................................................................................................................. OFF
Cache ............................................................................................................................................
Config .................................................................................................................................... CACHED
Events ................................................................................................................................ NOT CACHED
Routes ................................................................................................................................ NOT CACHED
Views ..................................................................................................................................... CACHED
Drivers ..........................................................................................................................................
Broadcasting .............................................................................................................................. pusher
Cache ...................................................................................................................................... redis
Database ................................................................................................................................... mysql
Logs .............................................................................................................................. stack / single
Mail ........................................................................................................................................ smtp
Queue ....................................................................................................................................... sync
Session .................................................................................................................................... redis
Ответы (1 шт):
Автор решения: Hopex Development
→ Ссылка
Проблему удалось решить следующим способом.
В классе Encrypter
есть следующие определения:
private static $supportedCiphers = [
'aes-128-cbc' => ['size' => 16, 'aead' => false],
'aes-256-cbc' => ['size' => 32, 'aead' => false],
'aes-128-gcm' => ['size' => 16, 'aead' => true],
'aes-256-gcm' => ['size' => 32, 'aead' => true],
];
Так как я использую aes-256-cbc
, ключ должен иметь длину 32, а мой текущий ключ имеет длину 51. Решение-костыль:
$newEncrypter = new \Illuminate\Encryption\Encrypter(substr(config('app.key'), 0, 32), config('app.cipher'));
Если кто-то знает более правильное решение, прошу об этом сообщить, буду признателен.