New article: Trubanc Server Working
[loomclient.git] / Cipher.php
blobf6c4ff13b73b2698b16a33efddc66d684e60f939
1 <?php
3 /*
4 * Some AES encryption. Works in PHP 4 or 5.
5 * Scrounged on the web, and made to work.
6 */
8 class Cipher {
9 var $securekey, $iv;
10 var $enc, $mode;
12 function Cipher($textkey) {
13 $this->enc = MCRYPT_RIJNDAEL_128;
14 $this->mode = MCRYPT_MODE_ECB;
15 $this->securekey = mhash(MHASH_SHA256,$textkey);
16 $iv_size = mcrypt_get_iv_size($this->enc, $this->mode);
17 $this->iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_RANDOM);
20 function encrypt($input) {
21 return mcrypt_encrypt($this->enc, $this->securekey, $input, $this->mode, $this->iv);
24 function decrypt($input) {
25 return trim(mcrypt_decrypt($this->enc, $this->securekey, $input, $this->mode, $this->iv));
28 function encrypt2hex($input) {
29 return bin2hex($this->encrypt($input));
32 function decrypthex($input) {
33 return $this->decrypt(pack("H*", $input));
37 /* test code
38 $cipher = new Cipher('secret passphrase');
40 $encryptedtext = $cipher->encrypt2hex(pack("H*", "1650f617c024d6441461b2538c6d9540"));
41 echo "->encrypt = $encryptedtext<br />";
43 $decryptedtext = bin2hex($cipher->decrypthex($encryptedtext));
44 echo "->decrypt = $decryptedtext<br />";
46 var_dump($cipher);