在网址中发送帖子数据并加以保护

时间:2015-06-03 07:36:53

标签: php security url encryption

我想在网址中发送一些信息,但我怎样才能以安全的方式加密?

这是我的网址,例如: http://www.domain.com/process/?var=1&variable=2

如果用户知道该网址,他可以更改变量,而这不是意图。

我必须加密它,并在我认为的服务器上解密它。但是这样做的好方法是什么?

使用更多信息进行编辑

我要归档的是我的手机应用程序(在这种情况下是IOS)通过使用API​​与我的网络应用程序(主要是)进行通信。 URL可能如下所示 http://www.website.com/api/my_secret_key/get/users,它将以json回应 此外,还需要由应用设置值,例如:http://www.website.com/api/my_secret_key/set/user/score/100

然而,使用上述URL调整值很容易,但不需要。如何使用安全的方法来完成。

1 个答案:

答案 0 :(得分:-2)

  

警告:以下是不安全的加密代码。 A better answer and solution already exists on StackOverflow

class encryption{
        private $config;

        public function __construct( $options=array() ){
            $this->config=array_merge(
                array(
                    'cipher'    =>  MCRYPT_RIJNDAEL_256,
                    'mode'      =>  MCRYPT_MODE_ECB,
                    'key'       =>  FALSE,
                    'iv'        =>  FALSE,
                    'size'      =>  FALSE,
                    'base64'    =>  TRUE,
                    'salt'      =>  FALSE
                ),
                $options
            );
        }
        private function getivs( $config=object ){
            $config->size=mcrypt_get_iv_size( $config->cipher, $config->mode );
            $config->iv=mcrypt_create_iv( $config->size, MCRYPT_RAND );
        }
        public function encrypt( $data=NULL ){
            $config=(object)$this->config;
            $this->getivs( $config );
            $data=trim( $data );
            $module = mcrypt_module_open( $config->cipher, '', $config->mode, '' );
            mcrypt_generic_init( $module, $config->key, $config->iv );

            $output = $config->base64 ? base64_encode( mcrypt_generic( $module, $data ) ) : mcrypt_generic( $module, $data );

            mcrypt_generic_deinit( $module );
            mcrypt_module_close( $module );
            return $output;
        }
        public function decrypt( $data=NULL ){
            $config=(object)$this->config;
            $this->getivs( $config );
            mb_detect_order( 'auto' );
            $encoding=mb_detect_encoding( $data );
            if( !$data or is_null( $data ) or empty( $data ) or !$encoding or $data=='' or base64_decode( $data )=='' ) return FALSE;

            $module = mcrypt_module_open( $config->cipher, '', $config->mode, '' );
            mcrypt_generic_init( $module, $config->key, $config->iv );

            $output = $config->base64 ? rtrim( mdecrypt_generic( $module, base64_decode( $data ) ),"\0" ) : rtrim( mdecrypt_generic( $module, $data ),"\0" );

            mcrypt_generic_deinit( $module );
            mcrypt_module_close( $module );
            return urldecode( $output );
        }
}//end class




/* Prepare data for transmission */
$enc=new encryption(array('key'=>'s0m3v3ryr4nd0mt3xt'));
$payload=enc->encrypt('var1=value1&var2=value2&var3=value3');

$url='http://www.domain.com/process/?payload='.$payload;


/* to decrypt */
$payload=$_GET['payload'];
$decrypted=$enc->decrypt( $payload );

/* process querystring - explode, split whatever.. */
相关问题