user4489379
user4489379

Reputation:

PHP: Create a iv. Not working

I have write a code to encrypt a data with mcrypt. When I create a IV it works BUT when I decrypt it the decrypted text isn't the same as before and it gives me this error:

Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize

Here is my code when I create a IV:

$iv = mcrypt_create_iv(mcrypt_get_iv_size($algo, $type), MCRYPT_RAND);

The problem is also that it gives a string with unreadable characters.

Here is my code when encrypting:

trim(base64_encode(mcrypt_encrypt($algo, $pass,
                                  $data, $type, $iv)));

And here is for decrypting:

trim(mcrypt_decrypt($algo, $pass,
                    base64_decode($data), $type, $iv));

The question: Why can't it decrypt so I get the plaintext and why it gives me a error.

EDIT: When I use a EMPTY IV, it works and I get the plaintext, but it still gives me error:

Warning: mcrypt_xxxxxx(): The IV parameter must be as long as the blocksize 

Upvotes: 1

Views: 145

Answers (1)

Scott Arciszewski
Scott Arciszewski

Reputation: 34113

I have write a code to encrypt a data with mcrypt.

This never bodes well. (Emphasis mine.)

When I create a IV it works BUT when I decrypt it the decrypted text isn't the same as before and it gives me this error:

Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize

Here is my code when I create a IV:

$iv = mcrypt_create_iv(mcrypt_get_iv_size($algo, $type), MCRYPT_RAND);

Do you know what MCRYPT_RAND means? It means rand(), an insecure random number generator.

Every algorithm that requires an initialization vector (IV) has the following security requirements:

  1. Initialization vectors must never repeat.
  2. Initialization vectors must be unpredictable.

MCRYPT_RAND fails at both counts: There are only 2^32 possible outputs from rand() and it's predictable.

The question: Why can't it decrypt so I get the plaintext and why it gives me a error.

The simplest answer is mcrypt sucks and you should use something else. My recommendations are here.

Upvotes: 0

Related Questions