Php 通过url加密字符串并在下一页检索?

Php 通过url加密字符串并在下一页检索?,php,encryption,Php,Encryption,当我通过url传递字符串时,我使用以下php脚本对其进行加密 我的大部分代码存储在encryption.php文件中 encryption.php: <?php class encryption{ private $config; public function __construct( $options=array() ){ $this->config=array_merge( array(

当我通过url传递字符串时,我使用以下php脚本对其进行加密

我的大部分代码存储在encryption.php文件中

encryption.php:

<?php 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

?>

然后我调用我的函数,并通过我的track_request.php文件中的url将我的字符串作为加密字符串传递:

require_once 'dependables/encryption.php';
        $string = 'NS' . substr( md5(rand()), 0, 7);
        $enc=new encryption( array( 'key'=>'PlowFish' ) );
        $encrypted_string = $enc->encrypt( $string );


        echo '<a href="ns_application.php?ns_request='.$encrypted_string.'">Click</a>
require_once'releases/encryption.php';
$string='NS'。substr(md5(rand()),0,7);
$enc=新加密(数组('key'=>'PlowFish');
$encrypted_string=$enc->encrypt($string);
回声'
最后,我检索加密字符串并在我的ns_application.php页面上再次解密:

require_once 'dependables/encryption.php';
$reference = isset($_GET['ns_request']) ? $_GET['ns_request'] : null;
$enc=new encryption( array( 'key'=>'PlowFish' ) );

$decrypted_string=$enc->decrypt( $reference ); ?>


<?php echo $decrypted_string; ?>
require_once'releases/encryption.php';
$reference=isset($\u GET['ns\u request'])$_GET['ns_request']:null;
$enc=新加密(数组('key'=>'PlowFish');
$decrypted_string=$enc->decrypt($reference);?>
问题是这个脚本10次工作5次,我将得到一个解密字符串,看起来像“NS1H57347”或“NS197G347”等等

然而,其他5次它将失败,并且根本不会解密,因此我最终得到一个非常长的加密字符串,如:“FJQRU248T\$90=}\31232HGfd*,^=”


有人能告诉我哪里出了问题吗?谢谢

您没有对字符串进行url编码,因此加密字符串的二进制“垃圾”中存在的任何url元字符都将在接收端进行解码。为什么不将其放入会话中,那就没有加密了needed@Dagon谢谢你的建议,但是如果我能把这个给你的话,我宁愿不使用会话work@MarcB有正确的ansewer,但是用这种方式加密似乎太过分了。如果加密+编码后的数据大于2K,则会出现故障。如果您需要接近这一点,即使您使用cookie而不是会话或其他类型的缓存,您也会增加页面的加载时间(同样,这取决于数据大小)。最后,它需要加密吗?如果你只使用HTTPS,你和最终用户是唯一知道url中内容的人。“如果我能让它工作,我宁愿不使用会话”。。。为什么?