note 94125 added to function.mcrypt-decrypt
| From: | uramihsayibok at osu1 dot php dot net | Date: | Sun, 18 Oct 2009 06:17:25 +0000 |
| Subject: | note 94125 added to function.mcrypt-decrypt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161844@lists.php.net to get a copy of this message | ||
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...
----
Server IP: 192.41.40.227
Probable Submitter: 69.166.47.131
----
Manual Page -- http://www.php.net/manual/en/function.mcrypt-decrypt.php
Edit -- https://master.php.net/note/edit/94125
Del: integrated -- https://master.php.net/note/delete/94125/integrated
Del: useless -- https://master.php.net/note/delete/94125/useless
Del: bad code -- https://master.php.net/note/delete/94125/bad+code
Del: spam -- https://master.php.net/note/delete/94125/spam
Del: non-english -- https://master.php.net/note/delete/94125/non-english
Del: in docs -- https://master.php.net/note/delete/94125/in+docs
Del: other reasons-- https://master.php.net/note/delete/94125
Reject -- https://master.php.net/note/reject/94125
Search -- https://master.php.net/manage/user-notes.php