Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fix openssl_encrypt(): Using an empty Initialization Vector (iv) is potentially insecure and not recommended in laravel?

I tried to DES encryption a Query String but it give this error.

<?php
class DES
{
    var $key;
    var $iv;
    function DES( $key, $iv=0 ) {
        $this->key = $key;
        if( $iv == 0 ) {
            $this->iv = $key;
        } else {
            $this->iv = $iv;
        }
    }

    function encrypt($str) {
        return base64_encode( openssl_encrypt($str, 'DES-CBC', $this->key, OPENSSL_RAW_DATA, $this->iv  ) );
    }

}
?>

$str="method=GetUserStatusDV&Key=01234567789ABCDEF0123456789ABCDE&Time=20150101012345&Username=abd12345"; // for example
    $key = 'ZyXw4321'; // for example
    $crypt = new DES($key);
    $mstr = $crypt->encrypt($str);
    $urlemstr = urlencode($mstr);
    echo "[ $str ] Encrypted: [ $mstr ] UrlEncoded encrypted string: [ $urlemstr ]";

openssl_encrypt(): Using an empty Initialization Vector (iv) is potentially insecure and not recommended

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Even though you are using a deprecated way to instantiate your class (using same name instead of __construct method) the sample code you provided is working.

You can improve your class like this.

class DES
{
    private $key;
    private $iv;
    public function __construct(string $key, string $iv = '') {
        $this->key = $key;
        if(strlen($iv) != 8) {
            $this->iv = \Illuminate\Support\Str::random(8);
        } else {
            $this->iv = $iv;
        }
    }

    public function encrypt($str) {
        return base64_encode( openssl_encrypt($str, 'DES-CBC', $this->key, OPENSSL_RAW_DATA, $this->iv  ) );
    }

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading