...

View Full Version : Mcrypt_rijindael



werty37
06-14-2006, 08:43 AM
I rewrote a small php script into class file.
When i execute the code, I get this error

Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /var/www/emites/securedata_inc.php on line 3



class securedata
{
var $key = "Four score and twenty years ago";
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND);

function encrypt($STR)
{
return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $STR, MCRYPT_MODE_CBC, $iv);
}

function decrypt($STR)
{
return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $STR, MCRYPT_MODE_CBC, $iv);
}

}

$sd = new securedata();
$enc = $sd->encrypt('SUJITH');
echo bin2hex($enc);
echo "<br>";
echo ($sd->decrypt($enc));

ralph l mayo
06-14-2006, 08:54 AM
The specific problem is that only trivial assignments are allowed outside of methods in classes. Initial values should be assigned in the construction method. In PHP5 it looks something like this:



class securedata
{
private
$key,
$iv;

public function __construct()
{
$this->key = 'Four score and twenty years ago';
$this->iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND);
}

public function encrypt($STR)
{
return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, $STR, MCRYPT_MODE_CBC, $this->iv);
}

public function decrypt($STR)
{
return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, $STR, MCRYPT_MODE_CBC, $this->iv);
}
}

werty37
06-14-2006, 06:15 PM
I luv you ralph l mayo. It works like charm...
I ve been banging my head for a long time.
You saved me.

Thank You
werty37



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum