component issue

IT’s a what didn’t i get and did wrong topic. The guide isn’t telling me everything I think or i screwed up big time but i get this:


Property "DefaultController.Encrypt" is not defined.

my code in the config/main.php file is:


....

'encrypt'=>array("encryptionKey"=> "EdA45D21LDOGNJGJROS0I356?F833NC7DN4KQ9109D"),

.....

my extention code is the encrypt module from codeigniter that i ported to yii:


<?php

/**

 * Encryption library based upon the CI encrypt library

 *

 *

 */ 


class Encrypt extends CApplicationComponent {


	/**

	 * @var array the configuration

	 */

	public $encryptionKey;

	var $_hash_type	= 'sha1';

	var $_mcrypt_exists = FALSE;

	var $_mcrypt_cipher;

	var $_mcrypt_mode;


	/**

	 * Constructor

	 *

	 * Initializes the application component.

	 */

	public function init()

	{

		parent::init();

		$this->processRules();

	}


	// --------------------------------------------------------------------


	/**

	 * Fetch the encryption key

	 *

	 * Returns it as MD5 in order to have an exact-length 128 bit key.

	 * Mcrypt is sensitive to keys that are not the correct length

	 *

	 * @access	public

	 * @param	string

	 * @return	string

	 */

	function getKey($encryptionKey = '')

	{

		if ($encryptionKey == '')

		{

			if ($this->encryptionKey != '')

			{

				return $this->encryptionKey;

			}


			$key = $encryptionKey;


			if ($key === FALSE)

			{

				show_error('In order to use the encryption class requires that you set an encryption key in your config file.');

			}

		}


		return md5($encryptionKey);

	}


	// --------------------------------------------------------------------


	/**

	 * Set the encryption key

	 *

	 * @access	public

	 * @param	string

	 * @return	void

	 */

	function setKey($key = '')

	{

		$this->encryptionKey = $key;

	}


	// --------------------------------------------------------------------


	/**

	 * Encode

	 *

	 * Encodes the message string using bitwise XOR encoding.

	 * The key is combined with a random hash, and then it

	 * too gets converted using XOR. The whole thing is then run

	 * through mcrypt (if supported) using the randomized key.

	 * The end result is a double-encrypted message string

	 * that is randomized with each call to this function,

	 * even if the supplied message and key are the same.

	 *

	 * @access	public

	 * @param	string	the string to encode

	 * @param	string	the key

	 * @return	string

	 */

	function encode($string, $key = '')

	{

		$key = $this->get_key($key);

		$enc = $this->_xor_encode($string, $key);

		

		if ($this->_mcrypt_exists === TRUE)

		{

			$enc = $this->mcrypt_encode($enc, $key);

		}

		return base64_encode($enc);

	}


	// --------------------------------------------------------------------


	/**

	 * Decode

	 *

	 * Reverses the above process

	 *

	 * @access	public

	 * @param	string

	 * @param	string

	 * @return	string

	 */

	function decode($string, $key = '')

	{

		$key = $this->get_key($key);

		

		if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string))

		{

			return FALSE;

		}


		$dec = base64_decode($string);


		if ($this->_mcrypt_exists === TRUE)

		{

			if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE)

			{

				return FALSE;

			}

		}


		return $this->_xor_decode($dec, $key);

	}


	// --------------------------------------------------------------------


	/**

	 * XOR Encode

	 *

	 * Takes a plain-text string and key as input and generates an

	 * encoded bit-string using XOR

	 *

	 * @access	private

	 * @param	string

	 * @param	string

	 * @return	string

	 */

	function _xor_encode($string, $key)

	{

		$rand = '';

		while (strlen($rand) < 32)

		{

			$rand .= mt_rand(0, mt_getrandmax());

		}


		$rand = $this->hash($rand);


		$enc = '';

		for ($i = 0; $i < strlen($string); $i++)

		{			

			$enc .= substr($rand, ($i % strlen($rand)), 1).(substr($rand, ($i % strlen($rand)), 1) ^ substr($string, $i, 1));

		}


		return $this->_xor_merge($enc, $key);

	}


	// --------------------------------------------------------------------


	/**

	 * XOR Decode

	 *

	 * Takes an encoded string and key as input and generates the

	 * plain-text original message

	 *

	 * @access	private

	 * @param	string

	 * @param	string

	 * @return	string

	 */

	function _xor_decode($string, $key)

	{

		$string = $this->_xor_merge($string, $key);


		$dec = '';

		for ($i = 0; $i < strlen($string); $i++)

		{

			$dec .= (substr($string, $i++, 1) ^ substr($string, $i, 1));

		}


		return $dec;

	}


	// --------------------------------------------------------------------


	/**

	 * XOR key + string Combiner

	 *

	 * Takes a string and key as input and computes the difference using XOR

	 *

	 * @access	private

	 * @param	string

	 * @param	string

	 * @return	string

	 */

	function _xor_merge($string, $key)

	{

		$hash = $this->hash($key);

		$str = '';

		for ($i = 0; $i < strlen($string); $i++)

		{

			$str .= substr($string, $i, 1) ^ substr($hash, ($i % strlen($hash)), 1);

		}


		return $str;

	}


	// --------------------------------------------------------------------


	/**

	 * Encrypt using Mcrypt

	 *

	 * @access	public

	 * @param	string

	 * @param	string

	 * @return	string

	 */

	function mcrypt_encode($data, $key)

	{

		$init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());

		$init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND);

		return $this->_add_cipher_noise($init_vect.mcrypt_encrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), $key);

	}


	// --------------------------------------------------------------------


	/**

	 * Decrypt using Mcrypt

	 *

	 * @access	public

	 * @param	string

	 * @param	string

	 * @return	string

	 */

	function mcrypt_decode($data, $key)

	{

		$data = $this->_remove_cipher_noise($data, $key);

		$init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());


		if ($init_size > strlen($data))

		{

			return FALSE;

		}


		$init_vect = substr($data, 0, $init_size);

		$data = substr($data, $init_size);

		return rtrim(mcrypt_decrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), "\0");

	}


	// --------------------------------------------------------------------


	/**

	 * Adds permuted noise to the IV + encrypted data to protect

	 * against Man-in-the-middle attacks on CBC mode ciphers

	 * http://www.ciphersbyritter.com/GLOSSARY.HTM#IV

	 *

	 * Function description

	 *

	 * @access	private

	 * @param	string

	 * @param	string

	 * @return	string

	 */

	function _add_cipher_noise($data, $key)

	{

		$keyhash = $this->hash($key);

		$keylen = strlen($keyhash);

		$str = '';


		for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)

		{

			if ($j >= $keylen)

			{

				$j = 0;

			}


			$str .= chr((ord($data[$i]) + ord($keyhash[$j])) % 256);

		}


		return $str;

	}


	// --------------------------------------------------------------------


	/**

	 * Removes permuted noise from the IV + encrypted data, reversing

	 * _add_cipher_noise()

	 *

	 * Function description

	 *

	 * @access	public

	 * @param	type

	 * @return	type

	 */

	function _remove_cipher_noise($data, $key)

	{

		$keyhash = $this->hash($key);

		$keylen = strlen($keyhash);

		$str = '';


		for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)

		{

			if ($j >= $keylen)

			{

				$j = 0;

			}


			$temp = ord($data[$i]) - ord($keyhash[$j]);


			if ($temp < 0)

			{

				$temp = $temp + 256;

			}

			

			$str .= chr($temp);

		}


		return $str;

	}


	// --------------------------------------------------------------------

	

	/**

	 * Set the Mcrypt Cipher

	 *

	 * @access	public

	 * @param	constant

	 * @return	string

	 */

	function set_cipher($cipher)

	{

		$this->_mcrypt_cipher = $cipher;

	}


	// --------------------------------------------------------------------


	/**

	 * Set the Mcrypt Mode

	 *

	 * @access	public

	 * @param	constant

	 * @return	string

	 */

	function set_mode($mode)

	{

		$this->_mcrypt_mode = $mode;

	}


	// --------------------------------------------------------------------


	/**

	 * Get Mcrypt cipher Value

	 *

	 * @access	private

	 * @return	string

	 */

	function _get_cipher()

	{

		if ($this->_mcrypt_cipher == '')

		{

			$this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256;

		}


		return $this->_mcrypt_cipher;

	}


	// --------------------------------------------------------------------


	/**

	 * Get Mcrypt Mode Value

	 *

	 * @access	private

	 * @return	string

	 */

	function _get_mode()

	{

		if ($this->_mcrypt_mode == '')

		{

			$this->_mcrypt_mode = MCRYPT_MODE_ECB;

		}

		

		return $this->_mcrypt_mode;

	}


	// --------------------------------------------------------------------


	/**

	 * Set the Hash type

	 *

	 * @access	public

	 * @param	string

	 * @return	string

	 */

	function set_hash($type = 'sha1')

	{

		$this->_hash_type = ($type != 'sha1' AND $type != 'md5') ? 'sha1' : $type;

	}


	// --------------------------------------------------------------------


	/**

	 * Hash encode a string

	 *

	 * @access	public

	 * @param	string

	 * @return	string

	 */	

	function hash($str)

	{

		return ($this->_hash_type == 'sha1') ? $this->sha1($str) : md5($str);

	}


	// --------------------------------------------------------------------


	/**

	 * Generate an SHA1 Hash

	 *

	 * @access	public

	 * @param	string

	 * @return	string

	 */

	function sha1($str)

	{

		if ( ! function_exists('sha1'))

		{

			if ( ! function_exists('mhash'))

			{

				require_once(BASEPATH.'libraries/Sha1'.EXT);

				$SH = new CI_SHA;

				return $SH->generate($str);

			}

			else

			{

				return bin2hex(mhash(MHASH_SHA1, $str));

			}

		}

		else

		{

			return sha1($str);

		}

	}


}

my exemple code:

<?php echo $this->Encrypt->encode("test"); ?>

MY question is why doesn’t yii want to load it, do I need to load the module by a command or is it the code that has issues?

please help me, this is important because it’s the basis of a my encryption system.

Thank you,

Tim

Try this




....

'encrypt'=>array(

  'class'=>'Encrypt',

  'encryptionKey'=> 'EdA45D21LDOGNJGJROS0I356?F833NC7DN4KQ9109D'),

.....



/Tommy

Unfortunately it doesn’t work that way either :( and my file is in the map /extensions.

It is Yii::app()->encrypt rather than $this->encrypt

Thanks that solves one thing but now i get : [s]Property "CWebApplication.Encrypt" is not defined.

what do I still need to extend or implement to be able to use it, i thought i had everything concerning the extension?[/s]

My mistake, had do give it a ‘e’ not an ‘E’ still have an error with get_key on line 95 but my functions are all good now.

I get a annoying error and basically i don’t know where it comes from is it yii or is it the component self.

The component::


<?php

/**

 * Encryption library based upon the CI encrypt library

 *

 *

 */ 


class Encrypt extends CApplicationComponent {


	/**

	 * @var array the configuration

	 */

	public $encryptionKey;

	var $_hash_type	= 'sha1';

	var $_mcrypt_exists = FALSE;

	var $_mcrypt_cipher;

	var $_mcrypt_mode;


	/**

	 * Constructor

	 *

	 * Initializes the application component.

	 */

	public function init()

	{

		parent::init();

	}


	// --------------------------------------------------------------------


	/**

	 * Fetch the encryption key

	 *

	 * Returns it as MD5 in order to have an exact-length 128 bit key.

	 * Mcrypt is sensitive to keys that are not the correct length

	 *

	 * @access	public

	 * @param	string

	 * @return	string

	 */

	function getKey($encryptionKey = '')

	{

		if ($encryptionKey == '')

		{

			if ($this->encryptionKey != '')

			{

				return $this->encryptionKey;

			}


			$key = $encryptionKey;


			if ($key === FALSE)

			{

				show_error('In order to use the encryption class requires that you set an encryption key in your config file.');

			}

		}


		return md5($encryptionKey);

	}


	// --------------------------------------------------------------------


	/**

	 * Set the encryption key

	 *

	 * @access	public

	 * @param	string

	 * @return	void

	 */

	function setKey($key = '')

	{

		$this->encryptionKey = $key;

	}


	// --------------------------------------------------------------------


	/**

	 * Encode

	 *

	 * Encodes the message string using bitwise XOR encoding.

	 * The key is combined with a random hash, and then it

	 * too gets converted using XOR. The whole thing is then run

	 * through mcrypt (if supported) using the randomized key.

	 * The end result is a double-encrypted message string

	 * that is randomized with each call to this function,

	 * even if the supplied message and key are the same.

	 *

	 * @access	public

	 * @param	string	the string to encode

	 * @param	string	the key

	 * @return	string

	 */

	function encode($string, $key = '')

	{

		$key = $this->getKey($key);

		$enc = $this->_xor_encode($string, $key);

		

		if ($this->_mcrypt_exists === TRUE)

		{

			$enc = $this->mcrypt_encode($enc, $key);

		}

		return base64_encode($enc);

	}


	// --------------------------------------------------------------------


	/**

	 * Decode

	 *

	 * Reverses the above process

	 *

	 * @access	public

	 * @param	string

	 * @param	string

	 * @return	string

	 */

	function decode($string, $key = '')

	{

		$key = $this->getKey($key);

		

		if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string))

		{

			return FALSE;

		}


		$dec = base64_decode($string);


		if ($this->_mcrypt_exists === TRUE)

		{

			if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE)

			{

				return FALSE;

			}

		}


		return $this->_xor_decode($dec, $key);

	}


	// --------------------------------------------------------------------


	/**

	 * XOR Encode

	 *

	 * Takes a plain-text string and key as input and generates an

	 * encoded bit-string using XOR

	 *

	 * @access	private

	 * @param	string

	 * @param	string

	 * @return	string

	 */

	function _xor_encode($string, $key)

	{

		$rand = '';

		while (strlen($rand) < 32)

		{

			$rand .= mt_rand(0, mt_getrandmax());

		}


		$rand = $this->hash($rand);


		$enc = '';

		for ($i = 0; $i < strlen($string); $i++)

		{			

			$enc .= substr($rand, ($i % strlen($rand)), 1).(substr($rand, ($i % strlen($rand)), 1) ^ substr($string, $i, 1));

		}


		return $this->_xor_merge($enc, $key);

	}


	// --------------------------------------------------------------------


	/**

	 * XOR Decode

	 *

	 * Takes an encoded string and key as input and generates the

	 * plain-text original message

	 *

	 * @access	private

	 * @param	string

	 * @param	string

	 * @return	string

	 */

	function _xor_decode($string, $key)

	{

		$string = $this->_xor_merge($string, $key);


		$dec = '';

		for ($i = 0; $i < strlen($string); $i++)

		{

			$dec .= (substr($string, $i++, 1) ^ substr($string, $i, 1));

		}


		return $dec;

	}


	// --------------------------------------------------------------------


	/**

	 * XOR key + string Combiner

	 *

	 * Takes a string and key as input and computes the difference using XOR

	 *

	 * @access	private

	 * @param	string

	 * @param	string

	 * @return	string

	 */

	function _xor_merge($string, $key)

	{

		$hash = $this->hash($key);

		$str = '';

		for ($i = 0; $i < strlen($string); $i++)

		{

			$str .= substr($string, $i, 1) ^ substr($hash, ($i % strlen($hash)), 1);

		}


		return $str;

	}


	// --------------------------------------------------------------------


	/**

	 * Encrypt using Mcrypt

	 *

	 * @access	public

	 * @param	string

	 * @param	string

	 * @return	string

	 */

	function mcrypt_encode($data, $key)

	{

		$init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());

		$init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND);

		return $this->_add_cipher_noise($init_vect.mcrypt_encrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), $key);

	}


	// --------------------------------------------------------------------


	/**

	 * Decrypt using Mcrypt

	 *

	 * @access	public

	 * @param	string

	 * @param	string

	 * @return	string

	 */

	function mcrypt_decode($data, $key)

	{

		$data = $this->_remove_cipher_noise($data, $key);

		$init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());


		if ($init_size > strlen($data))

		{

			return FALSE;

		}


		$init_vect = substr($data, 0, $init_size);

		$data = substr($data, $init_size);

		return rtrim(mcrypt_decrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), "\0");

	}


	// --------------------------------------------------------------------


	/**

	 * Adds permuted noise to the IV + encrypted data to protect

	 * against Man-in-the-middle attacks on CBC mode ciphers

	 * http://www.ciphersbyritter.com/GLOSSARY.HTM#IV

	 *

	 * Function description

	 *

	 * @access	private

	 * @param	string

	 * @param	string

	 * @return	string

	 */

	function _add_cipher_noise($data, $key)

	{

		$keyhash = $this->hash($key);

		$keylen = strlen($keyhash);

		$str = '';


		for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)

		{

			if ($j >= $keylen)

			{

				$j = 0;

			}


			$str .= chr((ord($data[$i]) + ord($keyhash[$j])) % 256);

		}


		return $str;

	}


	// --------------------------------------------------------------------


	/**

	 * Removes permuted noise from the IV + encrypted data, reversing

	 * _add_cipher_noise()

	 *

	 * Function description

	 *

	 * @access	public

	 * @param	type

	 * @return	type

	 */

	function _remove_cipher_noise($data, $key)

	{

		$keyhash = $this->hash($key);

		$keylen = strlen($keyhash);

		$str = '';


		for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)

		{

			if ($j >= $keylen)

			{

				$j = 0;

			}


			$temp = ord($data[$i]) - ord($keyhash[$j]);


			if ($temp < 0)

			{

				$temp = $temp + 256;

			}

			

			$str .= chr($temp);

		}


		return $str;

	}


	// --------------------------------------------------------------------

	

	/**

	 * Set the Mcrypt Cipher

	 *

	 * @access	public

	 * @param	constant

	 * @return	string

	 */

	function set_cipher($cipher)

	{

		$this->_mcrypt_cipher = $cipher;

	}


	// --------------------------------------------------------------------


	/**

	 * Set the Mcrypt Mode

	 *

	 * @access	public

	 * @param	constant

	 * @return	string

	 */

	function set_mode($mode)

	{

		$this->_mcrypt_mode = $mode;

	}


	// --------------------------------------------------------------------


	/**

	 * Get Mcrypt cipher Value

	 *

	 * @access	private

	 * @return	string

	 */

	function _get_cipher()

	{

		if ($this->_mcrypt_cipher == '')

		{

			$this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256;

		}


		return $this->_mcrypt_cipher;

	}


	// --------------------------------------------------------------------


	/**

	 * Get Mcrypt Mode Value

	 *

	 * @access	private

	 * @return	string

	 */

	function _get_mode()

	{

		if ($this->_mcrypt_mode == '')

		{

			$this->_mcrypt_mode = MCRYPT_MODE_ECB;

		}

		

		return $this->_mcrypt_mode;

	}


	// --------------------------------------------------------------------


	/**

	 * Set the Hash type

	 *

	 * @access	public

	 * @param	string

	 * @return	string

	 */

	function set_hash($type = 'sha1')

	{

		$this->_hash_type = ($type != 'sha1' AND $type != 'md5') ? 'sha1' : $type;

	}


	// --------------------------------------------------------------------


	/**

	 * Hash encode a string

	 *

	 * @access	public

	 * @param	string

	 * @return	string

	 */	

	function hash($str)

	{

		return ($this->_hash_type == 'sha1') ? $this->sha1($str) : md5($str);

	}


	// --------------------------------------------------------------------


	/**

	 * Generate an SHA1 Hash

	 *

	 * @access	public

	 * @param	string

	 * @return	string

	 */

	function sha1($str)

	{

		if ( ! function_exists('sha1'))

		{

			if ( ! function_exists('mhash'))

			{

				require_once(BASEPATH.'libraries/Sha1'.EXT);

				$SH = new CI_SHA;

				return $SH->generate($str);

			}

			else

			{

				return bin2hex(mhash(MHASH_SHA1, $str));

			}

		}

		else

		{

			return sha1($str);

		}

	}


}

The error:


CException

Description


Encrypt does not have a method named "get_key".

Source File


/var/www/framework/base/CComponent.php(218)


00206:      * @since 1.0.2

00207:      */

00208:     public function __call($name,$parameters)

00209:     {

00210:         if($this->_m!==null)

00211:         {

00212:             foreach($this->_m as $object)

00213:             {

00214:                 if($object->enabled && method_exists($object,$name))

00215:                     return call_user_func_array(array($object,$name),$parameters);

00216:             }

00217:         }

00218: throw new CException(Yii::t('yii','{class} does not have a method named "{name}".',

00219:             array('{class}'=>get_class($this), '{name}'=>$name)));

00220:     }

00221: 

00222:     /**

00223:      * Returns the named behavior object.

00224:      * The name 'asa' stands for 'as a'.

00225:      * @param string the behavior name

00226:      * @return IBehavior the behavior object, or null if the behavior does not exist

00227:      * @since 1.0.2

00228:      */

00229:     public function asa($behavior)

00230:     {


Stack Trace


#0 [internal function]: CComponent->__call('get_key', Array)

#1 /var/www/marchandise/protected/components/Encrypt.php(95): Encrypt->get_key('')

#2 /var/www/marchandise/protected/modules/users/views/default/index.php(9): Encrypt->encode('test')

#3 /var/www/framework/web/CBaseController.php(119): require('/var/www/marcha...')

#4 /var/www/framework/web/CBaseController.php(88): CBaseController->renderInternal('/var/www/marcha...', NULL, true)

#5 /var/www/framework/web/CController.php(701): CBaseController->renderFile('/var/www/marcha...', NULL, true)

#6 /var/www/framework/web/CController.php(640): CController->renderPartial('index', NULL, true)

#7 /var/www/marchandise/protected/modules/users/controllers/DefaultController.php(9): CController->render('index')

#8 /var/www/framework/web/actions/CInlineAction.php(32): DefaultController->actionIndex()

#9 /var/www/framework/web/CController.php(300): CInlineAction->run()

#10 /var/www/framework/web/CController.php(278): CController->runAction(Object(CInlineAction))

#11 /var/www/framework/web/CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)

#12 /var/www/framework/web/CWebApplication.php(332): CController->run('')

#13 /var/www/framework/web/CWebApplication.php(120): CWebApplication->runController('users')

#14 /var/www/framework/base/CApplication.php(134): CWebApplication->processRequest()

#15 /var/www/marchandise/index.php(11): CApplication->run()

#16 {main}




because it’s internal i go with the option that it’s the component but on line 95 i just got an accolade and nothing else and the get_key functions in the document were all replaced with getKey

Alright,

I know it is certainly not Yii and I’m certain it’s the code :P I don’t have a get_key() function anymore thus it shouldn’t annoy me. I don’t cache the application or anything so I’m really without clue as to what may cause this error.

I don’t know all details about PHP magic methods, but I suggest you try renaming your getKey method to something else.

/Tommy

Still I get the same error, nothing else :( but thanks for the tip

I got absolutely no idea why it bugged but i restarted my pc and apparently it now works fine. i’ll be posting the complete script only as soon as I got the chance to test it completely. And thanks for helping out everyone!

APC might be a good candidate for this issue… So if in doubt, always try to clean your APC or restart the webserver process.