【问题标题】:Encrypt string when passing it through url as hyperlink and decrypt to echo out on next page?通过 url 作为超链接传递字符串时加密字符串并解密以在下一页回显?
【发布时间】:2015-06-07 19:10:07
【问题描述】:

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

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

加密.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;

【问题讨论】:

  • @Mark harris 请检查我的答案。谢谢
  • 您在 ECB 模式(这是最差的模式)下使用 Rijndael-256(不是 AES 顺便说一句)并且您没有对密文进行身份验证。此加密库不安全。请参阅stackoverflow.com/a/30189841/2224584 了解安全实施。
  • @Mark harris 请也为答案投票

标签: php encryption


【解决方案1】:

问题是您已经向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。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 2011-12-07
  • 2015-04-20
  • 2020-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多