Yii driven URL encrypt and decrypt

Hi,

Is there anyone who can help me with full code for Url encrypt and decrypt function in a explanatory way so that i can use it for all URL GET parameters used in my website, something similar to the function mentioned below.

//In config

‘rules’ => array(

‘’ => ‘site/index’, // normal URL rules

array( // your custom URL handler

'class' => 'application.components.CustomUrlRule',

),

),

// Url encrpt decrypt - method

class CustomUrlRule extends CBaseUrlRule {

public function createUrl($manager,$route,$params,$ampersand) {

return your_encrypt_method($route); 

}

public function parseUrl($manager,$request,$pathInfo,$rawPathInfo) {

return your_decrypt_method($pathInfo);

}

}

///////////////////////////////////////////////////////////////

Many Thanks

Not sure how you are building your url. This is how I do it in my past projects.

Try something like this:

Encrypt

[b]

[/b]





        public function createUrl($dataObject)

	{

                // use security manager to encrypt 

		$security = Yii::app()->getSecurityManager();

		$encryptedProperty = $security->encrypt( $dataObject->property,  $user->someKey );

		$utf8Property = utf8_encode($encryptedProperty);

		$property = $utf8Property;

		

		// build url

		$route = implode(UserModule::module()->activateUrl);

		$params = array(

			'property'=>$property,

			'someKey'=>$someKey,

		);

		return $this->createAbsoluteUrl($route, $params);

	}



Decrypt

[b]

[/b]





        public function decryptUrlParam($data, $key)

	{

		$security = Yii::app()->getSecurityManager();

		return $security->decrypt( utf8_decode($data), $key );

	}


// use like this to parse the Url

$request = Yii::app()->getRequest();


$property = $request->getQuery('property');

$someKey = $request->getQuery('someKey');


$email = $this->decryptUrlParam($emailEncrypted, $activ8key);




This is hand typed coding, so watch out for errors.

Some lines may be omitted.

Check here for more.

Hope this helps.

My prop of encrypt and decrypt with options of method




<?php


/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */


/**

 * Description of CustomUrlRule

 *

 * @author w7600

 */

class CustomUrlRule extends CBaseUrlRule {


    var $skey = "KEYUNICA123456"; // you can change it


    public function createUrl($manager, $route, $params, $ampersand) {

        $paramString = array();

        $cosas = '';

        foreach ($params as $key => $value) {

            if (is_array($value)) {

                foreach ($value as $key2 => $value2) {

                    if ($value2 != NULL) {

                        $paramString[] = $key2;

                        $paramString[] = $value2;

                    }

                }

            } else {

                if ($value != NULL) {

                    $paramString[] = $key;

                    $paramString[] = $value;

                }

            }

        }

        $urlString = implode(",", $paramString);

        $paramStringEncoded = $urlString ? $this->encode($urlString) : '';

        return $route . '/' . $paramStringEncoded;

    }


    public function parseUrl($manager, $request, $pathInfo, $rawPathInfo) {

        $pathParams = explode("/", $pathInfo);

        if (isset($pathParams[2])) {

            $paramStringDecoded = $this->decode($pathParams[2]);

            $params = explode(",", $paramStringDecoded);

            for ($i = 0; $i < count($params); $i+= 2) {

                if (count($params) > ($i + 1)) {

                    $_GET[$params[$i]] = $params[$i + 1];

                    $_REQUEST[$params[$i]] = $params[$i + 1];

                } else {

                    $_GET[$params[$i]] = $params[$i];

                    $_REQUEST[$params[$i]] = $params[$i];

                }

            }

        }

        return $pathInfo;

    }


    public function safe_b64encode($string) {

        $data = base64_encode($string);

        $data = str_replace(array('+', '/', '='), array('-', '_', ''), $data);

        return $data;

    }


    public function safe_b64decode($string) {

        $data = str_replace(array('-', '_'), array('+', '/'), $string);

        $mod4 = strlen($data) % 4;

        if ($mod4) {

            $data .= substr('====', $mod4);

        }

        return base64_decode($data);

    }


    public function encode($value) {

//        if (!$value) {

//            return false;

//        }

//        $text = $value;

//        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);

//        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

//        $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->skey, $text, MCRYPT_MODE_ECB, $iv);

//        return trim($this->safe_b64encode($crypttext));

        return $this->mencrypt($value);

    }


    public function decode($value) {

//        if (!$value) {

//            return false;

//        }

//        $crypttext = $this->safe_b64decode($value);

//        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);

//        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

//        $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->skey, $crypttext, MCRYPT_MODE_ECB, $iv);

//        return trim($decrypttext);

        return $this->mdecrypt($value);

    }


    function mencrypt($input) {

        $key = substr(md5($this->skey), 0, 24);

        $td = mcrypt_module_open('tripledes', '', 'ecb', '');

        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);

        mcrypt_generic_init($td, $key, $iv);

        $encrypted_data = mcrypt_generic($td, $input);

        mcrypt_generic_deinit($td);

        mcrypt_module_close($td);

        return trim(chop($this->url_base64_encode($encrypted_data)));

    }


    function mdecrypt($input) {

        $input = trim(chop($this->url_base64_decode($input)));

        $td = mcrypt_module_open('tripledes', '', 'ecb', '');

        $key = substr(md5($this->skey), 0, 24);

        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);

        mcrypt_generic_init($td, $key, $iv);

        $decrypted_data = mdecrypt_generic($td, $input);

        mcrypt_generic_deinit($td);

        mcrypt_module_close($td);

        return trim(chop($decrypted_data));

    }


    function url_base64_encode($str) {

        return strtr(base64_encode($str), array(

            '+' => '.',

            '=' => '-',

            '/' => '~'

                )

        );

    }


    function url_base64_decode($str) {

        return base64_decode(strtr($str, array(

            '.' => '+',

            '-' => '=',

            '~' => '/'

                        )

        ));

    }


}


?>






I have use in encode&decode code it is working proper but when i am creating some module(in morethan sub part directory) at time in grid view update it is not working and it display Error 400

Your request is invalid. is display please give me to solution…

Specific URL Parse Issue

URL Encrypt/Decrypt is working fine however today i found one issue on specific URL.

Normal URL:

http://192.168.3.4/srv2/code/clients/view/17

Encrypted URL:

http://192.168.3.4/srv2/code/clients/view/g9tSPybz5g0-

Only on this URL make me crazy it gives ERROR 400 all other URL is working fine.

While Using skey var $skey = "KEYUNICA123456"; // you can change it

After change skey it works fine but i am not sure in future it may trouble with other specific URL or not

While checking issue it’s not decrypted above Encrypted URL

Code:


'rules' => array(

                    '' => 'site/index', // normal URL rules

                    'order'=>'order/index',

                    array( // your custom URL handler

                      'class' => 'application.components.CustomUrlRule',

                    ),

                    '<controller:\w+>/<id:\d+>' => '<controller>/view',

                    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',

                    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',

                    ),

please advice