Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Mcrypt_rijindael

    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

    Code:
    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));

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    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:

    PHP Code:
    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_128MCRYPT_MODE_ECB), MCRYPT_RAND);
        }
        
        public function 
    encrypt($STR)
        {
            return 
    mcrypt_encrypt(MCRYPT_RIJNDAEL_128$this->key$STRMCRYPT_MODE_CBC$this->iv);
        }

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


  • #3
    New Coder
    Join Date
    Jun 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •