Re: mcrypt don't work.
| From: | Adam Bregenzer | Date: | Sun, 08 Feb 2004 18:36:56 +0000 |
| Subject: | Re: mcrypt don't work. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-177161@lists.php.net to get a copy of this message | ||
On Sun, 2004-02-08 at 05:18, francesco@automationsoft.biz wrote:
> Hi all,
> I have problem with mcrypt function.
> There is always an error that I don't know how to correct this.
> This is my code:
>
> $string = "Text string";
> $key= "My key";
> $encrypted = mcrypt_cfb(MCRYPT_RIJNDAEL-256, $key, $string, MCRYPT_ENCRYPT);
> echo"stringa cifrata= $encrypted";
> $key = "My key";
> $string = mcrypt_cfb(MCRYPT_RIJNDAEL-256, $key, $encrypted, MCRYPT_DECRYPT);
> echo"stringa decifrata= $string";
If you are using mycrypt 2.4.x you need to do something like this:
encrypt:
# initialize the encryption module
$mc_module = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '',
MCRYPT_MODE_ECB, '');
mcrypt_generic_init($mc_module, $encryption_key, $iv);
# encrypt the data
$encrypted_data = mcrypt_generic($mc_module, $data);
# de-initialize the encryption module
mcrypt_generic_deinit($mc_module);
mcrypt_module_close($mc_module);
decrypt:
# initialize the encryption module
$mc_module = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '',
MCRYPT_MODE_ECB, '');
mcrypt_generic_init($mc_module, $encryption_key, $iv);
# decrypt the data
$decrypted_data = mdecrypt_generic($mc_module, $encrypted_data);
# trim any trailing \0, encrypted data is automatically padded to a
32byte boundary with \0
$decrypted_data = rtrim($decrypted_data);
# de-initialize the encryption module
mcrypt_generic_deinit($mc_module);
mcrypt_module_close($mc_module);
Check the documentation[1] for more information on encryption keys and
initialization vectors.
[1] http://www.php.net/mcrypt
--
Adam Bregenzer
adam@bregenzer.net
http://adam.bregenzer.net/