title: openssl_encrypt() randomly fails - IV passed is only ${x} bytes long, cipher expects an IV of precisely 16 bytes
body: <p>This is the code I use to encrypt/decrypt the data:</p>
  
  <pre class="lang-php prettyprint-override"><code>// Set the method
  $method = 'AES-128-CBC';
  
  // Set the encryption key
  $encryption_key = 'myencryptionkey';
  
  // Generet a random initialisation vector
  $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($method));
  
  // Define the date to be encrypted
  $data = "Encrypt me, please!";
  
  var_dump("Before encryption: $data");
  
  // Encrypt the data
  $encrypted = openssl_encrypt($data, $method, $encryption_key, 0, $iv);
  
  var_dump("Encrypted: ${encrypted}");
  
  // Append the vector at the end of the encrypted string
  $encrypted = $encrypted . ':' . $iv;
  
  // Explode the string using the `:` separator.
  $parts = explode(':', $encrypted);
  
  // Decrypt the data
  $decrypted = openssl_decrypt($parts[0], $method, $encryption_key, 0, $parts[1]);
  
  var_dump("Decrypted: ${decrypted}");
  </code></pre>
  
  <p>It ususaly works fine, but sometimes (1 in 10 or even less often) it fails. When it fails than the text is only partially encrypted:</p>
  
  <p>This is the error message when it happens:</p>
  
  <pre><code>Warning: openssl_decrypt(): IV passed is only 10 bytes long, cipher expects an IV of precisely 16 bytes, padding with \0
  </code></pre>
  
  <p>And when it happens the encrypted text looks like:</p>
  
  <pre><code>Encrypt me���L�se!
  </code></pre>
  
  <p>I thought that it might be caused by a bug in PHP, but I've tested on different hosts: PHP 7.0.6 and PHP 5.6. I've also tried multiple online PHP parsers like phpfidle.org or 3v4l.org.</p>
  
  <p>It seems that <code>openssl_random_pseudo_bytes</code> not always returns a string of a proper length, but I have no idea why.</p>
  
  <p>Here's the sample: <a href="https://3v4l.org/RZV8d" rel="noreferrer">https://3v4l.org/RZV8d</a></p>
  
  <p>Keep on refreshing the page, you'll get the error at some point.</p>