Laravel 5.7:openssl_cipher_iv_length():未知密码算法

时间:2018-11-15 16:24:31

标签: encryption laravel-5.7

我正在Laravel Framework 5.7.13中开发一个应用程序。

我有一堂课

<?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 ;

}
}

现在在我的控制器中,我做了:

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

在我的Config \ app.php

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

但是当我运行我的应用程序时,我得到了

ErrorException(E_WARNING) openssl_cipher_iv_length():未知密码算法

如何解决这个问题?

我试图注释Config \ app.php中的密码行,但随后出现了其他错误。

请帮助...

1 个答案:

答案 0 :(得分:0)

我在Laravel 5.7.13中遇到了类似的问题。

当我将WampServer安装更新到PHP v7.2.x(从v7.1.10开始)时,遇到了Laravel和openssl_cipher_iv_length()函数的错误。是的,我正在Windows上运行。

切换回php v7.1.10将清除错误。

要解决我的openssl_cipher_iv_length()错误,我比较了两个PHP版本中的php.ini文件。比较文件时,我注意到我没有正确设置extension_dir。这是我的主要问题,但是过去我还做了其他一些修改,这些修改也都整合到了新的PHP环境中(即,已启用的扩展和XDEBUG设置)。

此外...我确实注意到扩展名以前定义为: extension = php_ .dll

extension = .so

并且现在正在使用:

extension =

所以我的openssl_cipher_iv_length()问题是PHP版本而不是Laravel的结果。

希望这些信息对您有帮助。