note 43662 deleted from ref.mcrypt by cece
| From: | cece@php.net | Date: | Wed, 03 Nov 2004 02:14:27 +0000 |
| Subject: | note 43662 deleted from ref.mcrypt by cece | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-79527@lists.php.net to get a copy of this message | ||
Note Submitter: Mike Zaccari
----
I'm running PHP 4.3.7 on Apache 2.0.49 on an Xp machine, and after many hours of googling over
the internet I found that the easiest way to use the mcrypt function was to do this:
<?php
$key = "Secret Key";
//Encrypt Function
function encrypt($encrypt) {
global $key;
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $encrypt, MCRYPT_MODE_ECB, $iv);
$encode = base64_encode($passcrypt);
return $encode;
}
//Decrypt Function
function decrypt($decrypt) {
global $key;
$decoded = base64_decode($decrypt);
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_ECB, $iv);
return $decrypted;
}
?>
Notice the base64_encode, which makes it able to be inserted into MySQL easily. Now to encrypt just
run the encrypt("text");, and to decrypt use the decrypt("encrypted text");
I hope this helps anyone.