List Info

Thread: note 78531 added to function.mcrypt-encrypt




note 78531 added to function.mcrypt-encrypt
user name
2007-10-16 10:27:50
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  -- h
ttps://master.php.net/note/delete/78531/integrated
Del: useless     -- http
s://master.php.net/note/delete/78531/useless
Del: bad code    -- htt
ps://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     -- http
s://master.php.net/note/delete/78531/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78531
Reject      -- https://mast
er.php.net/note/reject/78531
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )