note 76479 deleted from function.crypt by danbrown
| From: | danbrown@php.net | Date: | Fri, 30 Sep 2011 21:34:26 +0000 |
| Subject: | note 76479 deleted from function.crypt by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183559@lists.php.net to get a copy of this message | ||
Note Submitter: sdc
----
ECB is one of the least secure block cipher modes (Wikipedia has some good information about these:
http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation)
because it is the only mode, out of the one's available for use with MCRYPT, that does not
actually use the IV provided. Note: PHP will emit an E_WARNING if the IV is missing or if its length
is not equal to the size mandated by mcrypt_get_iv_size().
In response to <addiakogiannis at isds dot gr>, even though the IV is randomly generated in
the encryption and decryption functions, the output remains the same given the same plaintext and
key because the mode is ECB. If a different mode is used, the IV must be preserved (say, by
concatenation with the ciphertext) in order for the ciphertext to be decrypted properly. That is:
<?php
//changing all instances of MCRYPT_MODE_ECB to MCRYPT_MODE_CFB
function RIJNDAEL_encrypt($text){
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$key = "This is a very secret key";
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CFB, $iv));
}
function RIJNDAEL_decrypt($text){
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$key = "This is a very secret key";
//I used trim to remove trailing spaces
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($text), MCRYPT_MODE_CFB, $iv));
}
//example
echo RIJNDAEL_decrypt(RIJNDAEL_encrypt('Her name was lola!'));
?>
...will output a random, 18-character string. Thus, the length of the plaintext is preserved, but
the data is completely corrupted.