note 43662 added to ref.mcrypt
| From: | MikeZaccari at rn2 dot php dot net | Date: | Tue, 29 Jun 2004 16:54:29 +0000 |
| Subject: | note 43662 added to ref.mcrypt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-72117@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/ref.mcrypt.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43662
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43662&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43662&report=yes
Search -- http://master.php.net/manage/user-notes.php