note 94125 deleted from function.mcrypt-decrypt by danbrown
| From: | danbrown@php.net | Date: | Fri, 30 Sep 2011 21:36:57 +0000 |
| Subject: | note 94125 deleted from function.mcrypt-decrypt by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183560@lists.php.net to get a copy of this message | ||
Note Submitter: uramihsayibok, gmail, com
----
kooktroop's example (09-Jul-2004) may be a bit misleading. Using a random IV for encryption,
then another random IV for decryption? If you run it, it works!
That's because ECB doesn't use an IV. For modes that do use one you need the same one for
encryption and decryption.
For example, Rijndael-256 (aka AES-256) and CFB:
<?php
$text = "The quick brown fox jumps over the lazy dog.";
$key = "haseveryletter";
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB), MCRYPT_RAND);
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CFB, $iv);
// create a new IV
$newiv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB), MCRYPT_RAND);
$corrupt = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_CFB, $newiv);
echo $corrupt; // [binary junk...]he lazy dog
$good = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_CFB, $iv);
echo $good; // The quick brown fox jumps over the lazy dog
?>
Rijndael can sync up on each block so only the first one (here, 256 bits = 32 characters long) will
be corrupted. Not necessarily true for other algorithms...