|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-07-15 01:47 UTC] lars at larswolter dot de
Description:
------------
Just changed from php4 to php5. Now mcrypt decryption does not work anymore. The function returns the encrypted data instead of the decrypted.
The testcode is the example from the mdecrypt_generic function documentation.
using php.ini-recommended and only changed the following
display_errors = On
display_startup_errors = On
extensions_dir = ...
loaded extra modules:
gd2, mbstring, mcrypt, mysql, exif
Reproduce code:
---------------
<?php
$key = 'this is a very long key...';
$plain_text = 'very important data';
$td = mcrypt_module_open('des', '', 'ecb','');
$key = substr($key, 0, mcrypt_enc_get_key_size($td));
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
if (mcrypt_generic_init($td, $key, $iv) != -1) {
$c_t = mcrypt_generic($td, $plain_text);
mcrypt_generic_deinit($td);
mcrypt_generic_init($td, $key, $iv);
$p_t = mdecrypt_generic($td, $c_t);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
if (strncmp($p_t, $plain_text, strlen($plain_text)) == 0)
echo "ok";
else
echo "error";
?>
Expected result:
----------------
the result should be: ok
Actual result:
--------------
the result is: error
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 08 11:00:01 2025 UTC |
Have had no problems here under Windows Server 2003. My test code that I used when a previous 4.x upgrade "fixed" a feature but broke my code, still works fine: <? function encrypt_pwd($string) { srand((double) microtime() * 1000000); $key = md5("bubbles"); $td = mcrypt_module_open('des', '','cfb', ''); $key = substr($key, 0, mcrypt_enc_get_key_size($td)); $iv_size = mcrypt_enc_get_iv_size($td); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); mcrypt_generic_init($td, $key, $iv); $c_t = mcrypt_generic($td, $string); mcrypt_generic_deinit($td); mcrypt_module_close($td); $c_t = $iv . $c_t; return $c_t; } function decrypt_pwd($string) { $key = md5("bubbles"); $td = mcrypt_module_open('des', '','cfb', ''); $key = substr($key, 0, mcrypt_enc_get_key_size($td)); $iv_size = mcrypt_enc_get_iv_size($td); $iv = substr($string, 0 ,$iv_size); $string = substr($string, $iv_size); mcrypt_generic_init($td, $key, $iv); $c_t = mdecrypt_generic($td, $string); mcrypt_generic_deinit($td); mcrypt_module_close($td); return $c_t; } $password = "muppet"; print "===".$password."===\n"; $password = encrypt_pwd($password); print "===".$password."===\n"; $password = decrypt_pwd($password); print "===".$password."===\n"; ?> Try this code and see what happens on your system. I found the examples on the site at times a bit misleading (or I was having a bad day), but one of the kind developers here showed me my error and the code above is what I now use, Taomyn