I have tried to follow the instructions in the PHP manual but have been
unable to get things working properly myself. The following script works
fine for me:
<?php
/* Open the cipher */
$td = mcrypt_module_o pen ('rijndael-256', '', 'ofb', '');
/* Create the IV and determine the keysize length */
$iv = mcrypt_create_i v (mcrypt_enc_get _iv_size($td),
MCRYPT_DEV_RAND OM);
$ks = mcrypt_enc_get_ key_size ($td);
/* Create key */
$key = substr (md5 ('very secret key'), 0, $ks);
/* Intialize encryption */
mcrypt_generic_ init ($td, $key, $iv);
/* Encrypt data */
$encrypted = mcrypt_generic ($td, 'This is very important data');
/* Terminate encryption handler */
mcrypt_generic_ deinit ($td);
/* Initialize encryption module for decryption */
mcrypt_generic_ init ($td, $key, $iv);
/* Decrypt encrypted string */
$decrypted = mdecrypt_generi c ($td, $encrypted);
/* Terminate decryption handle and close module */
mcrypt_generic_ deinit ($td);
mcrypt_module_c lose ($td);
/* Show string */
echo trim ($decrypted)."\ n";
?>
But, the encryption and decryption are all done in one shot - I've tried
to make two functions:
<?php
function do_encrypt ($string, $key) {
/* Open the cipher */
$td = mcrypt_module_o pen ('rijndael-256', '', 'ofb', '');
/* Create the IV and determine the keysize length */
$iv = mcrypt_create_i v (mcrypt_enc_get _iv_size($td),
MCRYPT_DEV_RAND OM);
$ks = mcrypt_enc_get_ key_size ($td);
/* Create key */
$key = substr (md5 ($key), 0, $ks);
/* Intialize encryption */
mcrypt_generic_ init ($td, $key, $iv);
/* Encrypt data */
$encrypted = mcrypt_generic ($td, 'This is very important data');
/* Terminate decryption handle and close module */
mcrypt_generic_ deinit ($td);
mcrypt_module_c lose ($td);
return $encrypted;
}
function do_decrypt ($string, $key) {
/* Open the cipher */
$td = mcrypt_module_o pen ('rijndael-256', '', 'ofb', '');
/* Create the IV and determine the keysize length */
$iv = mcrypt_create_i v (mcrypt_enc_get _iv_size($td),
MCRYPT_DEV_RAND OM);
$ks = mcrypt_enc_get_ key_size ($td);
/* Create key */
$key = substr (md5 ($key), 0, $ks);
/* Initialize encryption module for decryption */
mcrypt_generic_ init ($td, $key, $iv);
/* Decrypt encrypted string */
$decrypted = mdecrypt_generi c ($td, $string);
/* Terminate decryption handle and close module */
mcrypt_generic_ deinit ($td);
mcrypt_module_c lose ($td);
return $decrypted;
}
$encrypted_stri ng = do_encrypt("my string", "my key");
echo "The encrypted string is: " . $encrypted_stri ng . "<br>\n";
$decrypted_stri ng = do_decrypt($enc rypted_string, "my key");
echo "The decrypted string is: " . $decrypted_stri ng . "<br>\n";
?>
The thing is, the decrypted string never comes out as "my string"...
Can anyone help.
--
Pneu.....
http://www.pneumatus.co.uk
Release Your Mind