Hi

I'm trying to use a class from user notes in the php manual ( http://fr3.php.net/function.mcrypt_encrypt ) but i keep getting an error

the class is this :

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));
    }
}

and the error is this :

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in (...)/inc_config.php on line 704

this is line 704

private $securekey, $iv;

it's probably something really simple but i can't see it - can anyone help ?

thanks

    I suspect you are using an older version of PHP (pre PHP5), and thus the less complete object-oriented tools. Try changing private to var in your class variable declaration.

      yes that was the solution to that problem, thanks !!

      now i have another one :

      Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize on line 714

      this is line 714 (in the same class as above)

      return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->securekey, base64_decode($input), MCRYPT_MODE_ECB, $this->iv));

      I looked up blocksize in the manual but I can't understand what that's all about

      maybe I'm making life difficult for myself - all i want to do is encode some data (with a pre-defined key) before putting it in the db and using the same key to decode it for display - is there an easier way ?

        I' ve abandoned the class and now i'm using two functions i've made directly from the examples in the manual

        function encryptData($text, $key){
        	/*
        	$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
        	echo "<br /> size = ".$iv_size;
        	$iv = mcrypt_create_iv($iv_size);
        	echo "<br /> iv = ".$iv;
        	*/
        	$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, 'lIuKvV6mC9DU5HTKWWaczwlCRAkRNGkB');
        	return $crypttext;
        }
        
        function decryptData($text, $key){
        /*
        	$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
        	echo "<br /> size = ".$iv_size;
        	$iv = mcrypt_create_iv($iv_size);
        	echo "<br /> iv = ".$iv;
        */
        	$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, 'lIuKvV6mC9DU5HTKWWaczwlCRAkRNGkB');
        	return $decrypttext;
        }

        to update the database I use this

        $client_vars = encryptData($_POST['client_vars'], $_SESSION['key']);

        which results in this

        UPDATE clients SET client_name='AFP CONSEIL' , client_vars='‰~ ì8Þ*-…Ï !ј Ê¿æk ’â¬:Õ?Ž ´' WHERE client_id='31' LIMIT 1

        and in the db table i have this

        ‰~	ì8Þ*-…Ï
        !ј
        Ê¿æk ’â¬:Õ?Ž ´

        when i try to display it with this ($client_vars is the data from the mysql SELECT)

        bin2hex(decryptData($client_vars, $_SESSION['key']));

        i just get

        6a2a1ce12aed0e15959d98dc0f0698e054854d6f001ea49190c8f8ce9ec30464

        where am i going wrong ????!!!!!!!!

        helppppppp ..................

          Write a Reply...