我使用以下加密和解密脚本,以便当用户单击包含我的字符串的链接时,该字符串在url中加密并在下一页解密。
我的字符串正在被加密,但由于某种原因,它不会在下一页解密。谁能告诉我哪里错了吗?谢谢你,
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;
问题是您已经向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
.