Encryption mode requires an initialization vector of size 16

469 views Asked by At

I'm trying to encrypt a string using mcrypt_encrypt but I'm getting this warning Encryption mode requires an initialization vector of size 16. This is the code:

<?php
$str = "hassan";
$key = "WKSaRVxscxa7eDrX0mLjU6OUphS6F1z0";

$block = 16;
$pad = $block - (strlen($str) % $block);
$str .= str_repeat(chr($pad), $pad);


$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_CBC));

var_dump($encrypted);

And this is the output I'm getting

Warning: mcrypt_encrypt(): Encryption mode requires an initialization vector of size 16 in C:\laragon\www\info\index.php on line 10
string(0) ""

Please, help me!!!

1

There are 1 answers

0
Maarten Bodewes On

If you want to recreate the current operation then you should provide an IV consisting of 16 zero bytes (not just null or '0' characters). The all-zero IV was implicitly used before you had to provide the IV.

You are working with insecure code. The warning hasn't been added without reason, and mcrypt is an unmaintained library which desperately needs to be replaced. CBC is often not secure even if you provide a random IV.