通过url作为超链接传递字符串时加密字符串并解密以在下一页显示回显?

时间:2015-06-07 19:10:07

标签: php encryption

我正在使用以下加密和解密脚本,以便当用户点击包含我的字符串的链接时,它会在网址中加密并在下一页解密。

我的字符串正在加密查找但由于某种原因它不会在下一页解密。有人可以告诉我我哪里出错了?谢谢,

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

?>

new_supplier_listings.php:

session_start(); 
require_once 'dependables/encryption.php';

$string = 'NS12345';
        $enc=new encryption( array( 'key'=>'PlowFish' ) );
        $encrypted_string = $enc->encrypt( $string );


echo '<a href="ns_application.php?ns_request='.$encrypted_string.'">Click Here</a>';

ns_application.php:

require_once 'dependables/encryption.php'; 


$reference = isset($_GET['ns_request']) ? $_GET['ns_request'] : null;
$enc=new encryption( array( 'key'=>'PlowFish' ) );
$encrypted_string = $enc->encrypt( $reference );
echo $encrypted_string;
$decrypted_string=$enc->decrypt( $encrypted_string );
echo $decrypted_string;

1 个答案:

答案 0 :(得分:0)

问题是您已将加密值发送到ns_application.php。再次,你首先re-encrypt它和尝试decrypt它,这就是为什么它没有给你想要的结果。试试这个:

<强> ns_application.php

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once 'encryption.php'; 


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

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

输出:NS12345

注意: - 虽然您的代码非常精细,但最佳做法是添加error reporting code