note 30516 added to ref.mcrypt
| From: | m at ch9 dot us | Date: | Thu, 20 Mar 2003 20:05:56 +0000 |
| Subject: | note 30516 added to ref.mcrypt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-45569@lists.php.net to get a copy of this message | ||
I had some problems using the examples on mcrypt, so I wrote this class which is derivative of (paul
at xmill dot com)'s example:
*note that this example uses PHP 4.1.2 with lib mcrypt 2.5.x
<?php
class crypto_class {
function encrypt($string,$password) {
$td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, "");
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
mcrypt_generic_init ($td, $password, $iv);
$encrypted = mcrypt_generic ($td, $string);
mcrypt_generic_end ($td);
return($encrypted);
} // end encrypt
function decrypt($encrypted,$password) {
$td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, "");
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
mcrypt_generic_init ($td, $password, $iv);
$decrypted = mdecrypt_generic ($td, $encrypted);
mcrypt_generic_end ($td);
return($decrypted);
} //end decrypt
} // end crypto_class
$crypto = new crypto_class;
$encrypted = $crypto->encrypt('passwordx','good2Go123');
$decrypted = $crypto->decrypt($encrypted,'good2Go123');
print "$encrypted<br>\n";
print "$decrypted<br>\n";
?>
--
http://www.php.net/manual/en/ref.mcrypt.php
http://master.php.net/manage/user-notes.php?action=edit+30516
http://master.php.net/manage/user-notes.php?action=delete+30516
http://master.php.net/manage/user-notes.php?action=reject+30516