Home > Software design >  Laravel encrypter return error with generated key
Laravel encrypter return error with generated key

Time:11-10

I tried to encrypt value using Illuminate\Encryption\Encrypter but it returns an error.

Error:

{ "message": "Unsupported cipher or incorrect key length. Supported ciphers are: aes-128-cbc, aes-256-cbc, aes-128-gcm, aes-256-gcm.", "exception": "RuntimeException",....

code:

  $encrypter = new Encrypter("evsLYhBx1UAKKervjVIt1aJK0KLHHIf0vETUeI0HaYc=", config('app.cipher'));
  $encryptedValue = $encrypter->encryptString("test value");

My app.cipher

'cipher' => 'AES-256-CBC',

It returns an error that my key is not valid.

I also tried to generate key using this code but it's not working as well.

  $key = base64_encode(
    Encrypter::generateKey(config('app.cipher'))
  );

I also tried to copy the value of APP_KEY of .env key but not working too.

APP_KEY=base64:evsLYhBx1UAKKervjVIt1aJK0KLHHIf0vETUeI0HaYc=

How to generate a Key to use by lluminate\Encryption\Encrypter ?

CodePudding user response:

APP_KEY=base64:evsLYhBx1UAKKervjVIt1aJK0KLHHIf0vETUeI0HaYc=

The problem is with your key. Your app key is in base64 format so you have to decode when passing it as a parameter of Encrypter.

$encrypter = new Encrypter(base64_decode("evsLYhBx1UAKKervjVIt1aJK0KLHHIf0vETUeI0HaYc="), config('app.cipher'));  
$encryptedValue = $encrypter->encryptString("test value");
  • Related