note 78531 added to function.mcrypt-encrypt
| From: | dylan at wedefy dot com | Date: | Tue, 16 Oct 2007 15:27:50 +0000 |
| Subject: | note 78531 added to function.mcrypt-encrypt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131426@lists.php.net to get a copy of this message | ||
I wasn't too impressed with the suggested functions/classes for using mcrypt, so I wrote my own
class. The encypted output is base64 encoded so it can be used in URLs, emails, etc.
MCRYPT_RIJNDAEL_256 is probably too secure for most uses, using a less secure algorithm should mean
that the encryption will be faster and the encrypted output shorter (make sure to update iv_size in
mcrypt_create_iv() and key length to match the new algorithm). If you are going to use only 1
passphrase, you should define it inside __construct($this->securekey) instead of when creating
the object. Keep the class in a separate include file which is only readable by your webserver (or
whatever needs it) for added security.
<?php
class Cipher {
private $securekey, $iv;
function __construct($textkey) {
$this->securekey = hash('sha256',$textkey,TRUE);
$this->iv = mcrypt_create_iv(32);
}
function encrypt($input) {
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->securekey, $input,
MCRYPT_MODE_ECB, $this->iv));
}
function decrypt($input) {
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->securekey, base64_decode($input),
MCRYPT_MODE_ECB, $this->iv));
}
}
$cipher = new Cipher('secret passphrase');
$encryptedtext = $cipher->encrypt("hide me");
echo "->encrypt = $encryptedtext<br />";
$decryptedtext = $cipher->decrypt($encryptedtext);
echo "->decrypt = $decryptedtext<br />";
var_dump($cipher);
?>
----
Server IP: 216.235.15.211
Probable Submitter: 142.162.158.209
----
Manual Page -- http://www.php.net/manual/en/function.mcrypt-encrypt.php
Edit -- https://master.php.net/note/edit/78531
Del: integrated -- https://master.php.net/note/delete/78531/integrated
Del: useless -- https://master.php.net/note/delete/78531/useless
Del: bad code -- https://master.php.net/note/delete/78531/bad+code
Del: spam -- https://master.php.net/note/delete/78531/spam
Del: non-english -- https://master.php.net/note/delete/78531/non-english
Del: in docs -- https://master.php.net/note/delete/78531/in+docs
Del: other reasons-- https://master.php.net/note/delete/78531
Reject -- https://master.php.net/note/reject/78531
Search -- https://master.php.net/manage/user-notes.php