note 87526 modified in function.mcrypt-encrypt by danbrown
| From: | danbrown@php.net | Date: | Tue, 09 Dec 2008 05:08:15 +0000 |
| Subject: | note 87526 modified in function.mcrypt-encrypt by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147940@lists.php.net to get a copy of this message | ||
There's a lot of old information on this page, for those that just want some simple
encryption/decryption try this out.
<?php
define('SALT', 'whateveryouwant');
function encrypt($text)
{
return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, SALT, $text, MCRYPT_MODE_ECB,
mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}
function decrypt($text)
{
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, SALT, base64_decode($text), MCRYPT_MODE_ECB,
mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}
?>
then try out something like:
<?
$encryptedmessage = encrypt("You're my only hope!");
echo decrypt($encryptedmessage);
?>
It should return your original message. You can remove the base64_encode and base64_decode from the
functions if you want to save space on the encoded string, I use it to simplify database storage.
--was--
There's a lot of old information on this page, for those that just want some simple
encryption/decryption try this out.
<?
define('SALT', 'whateveryouwant');
function encrypt($text)
{
return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, SALT, $text, MCRYPT_MODE_ECB,
mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}
function decrypt($text)
{
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, SALT, base64_decode($text), MCRYPT_MODE_ECB,
mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}
?>
then try out something like:
<?
$encryptedmessage = encrypt("You're my only hope!");
echo decrypt($encryptedmessage);
?>
It should return your original message. You can remove the base64_encode and base64_decode from the
functions if you want to save space on the encoded string, I use it to simplify database storage.
http://php.net/manual/en/function.mcrypt-encrypt.php