MCrypt not decrypting simple text
| From: | Dan | Date: | Thu, 20 Mar 2008 21:08:31 +0000 |
| Subject: | MCrypt not decrypting simple text | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-271920@lists.php.net to get a copy of this message | ||
I'm using MCrypt and I have two very simple functions. All I'm doing is giving the function some text and a password, it encrypts the text and saves it as a text file on the server. Then I at some later time run another php file which decrypts using the decrypt function given the text and the SAME password as the first time and all I get is garbage. What am I doing wrong? What do I need to change so that I can get this to work the way I described?
function aes_128_encrypt($text,$password) {
$size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($size, MCRYPT_DEV_RANDOM);
$text .= chr(3).chr(3).chr(3);
return bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $password, $text, MCRYPT_MODE_ECB, $iv));} // End of function and function aes_128_decrypt($encrypted_text,$password) {
$size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($size, MCRYPT_DEV_RANDOM);
return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $password, pack("H*", $encrypted_text), MCRYPT_MODE_ECB, $iv);
} // End of function
- Dan