0
votes

I am using Laravel to decrypt a string encrypted from another application (also in Laravel) but I have a problem at the beginning.

I created a new object from \Illuminate\Encryption\Encrypter class in this way in order to use a different key instead the default one:

$new_encypter = new \Illuminate\Encryption\Encrypter("base64:ABCDEFGHIJKLF=", config('app.cipher'));

but I have this error:

The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.

The key that I used is a valid key because is from another Laravel application that works correctly and used the same encryption configuration.

The cipher passed to the constructor is correct because in the exception trace there is this line of code:

Illuminate\Encryption\Encrypter::__construct("base64:ABCDEFGHIJKLF=", "AES-256-CBC")

Where is the error?

I am using Laravel 6.

1

1 Answers

3
votes

The key is base64 encoded and prefixed with base64:. You would have to remove the prefix and base64 decode it.

This is how the EncryptionServiceProvider does it:

// get the app config
$config = $app->make('config')->get('app');

// see if the key starts with 'base64:'
if (Str::startsWith($key = $this->key($config), 'base64:')) {
    // decode the key
    $key = base64_decode(substr($key, 7));
}

return new Encrypter($key, $config['cipher']);

$this->key() just retrieves the key key from the config array.