Chaitally
Chaitally

Reputation: 25

Laravel 5.7: openssl_cipher_iv_length(): Unknown cipher algorithm

I am developing an app in Laravel Framework 5.7.13.

I have a class called

<?php
namespace App\Library;

class Crypto{

private $cipher;
private $cstrong;
private $keylen;
private $key;


public function __Crypto(){
    $this->cipher= Config::get('cipher');
    $this->cstrong = true;
    $this->keylen = 5;
    $this->key = bin2hex(openssl_random_pseudo_bytes($keylen, $cstrong));
}

public function opensslEncrypt($value){


    $ivlen = openssl_cipher_iv_length($this->cipher);
    $iv = openssl_random_pseudo_bytes($ivlen);
    $ciphertext_raw = openssl_encrypt($value, $this->cipher, $this->key, $options=OPENSSL_RAW_DATA, $iv);
    $hmac = hash_hmac('sha256', $ciphertext_raw, $this->key, $as_binary=true);
    $ciphertext = base64_encode( $iv.$hmac.$ciphertext_raw );

    return $ciphertext ;

}
}

Now in my controller I did:

$crypto = new Crypto();
$encryptedValue = $crypto->opensslEncrypt($orderId);

In my Config\app.php

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

But when I run my app, I get

ErrorException (E_WARNING) openssl_cipher_iv_length(): Unknown cipher algorithm

How to resolve this?

I tried to comment the cipher line in the Config\app.php, but then it gave some other errors.

Please help...

Upvotes: 0

Views: 6206

Answers (1)

AndrewF
AndrewF

Reputation: 11

I ran into a similar problem with Laravel 5.7.13.

My error with Laravel and the openssl_cipher_iv_length() function was encountered when I updated my WampServer installation to PHP v7.2.x (from v7.1.10). Yes, I am running on Windows.

Switching back to php v7.1.10 would clear the error.

To solve my error with openssl_cipher_iv_length(), I compared the php.ini files from the two php versions. When comparing the files I noticed that I did not have the extension_dir set properly. This was my main issue, but there were other edits I made in the past that I also incorporated into the new PHP environment (i.e. extensions that were enabled and XDEBUG settings).

Also... I did notice that the extension names were previously defined as: extension=php_<ext>.dll

or

extension=<ext>.so

and are now using:

extension=<ext>

So my issue with openssl_cipher_iv_length() was a result of the PHP version and not Laravel.

I hope this information helps.

Upvotes: 1

Related Questions