gpt4 book ai didi

php - 加密mysql数据并通过超链接,然后在下一页解密?

转载 作者:行者123 更新时间:2023-11-30 22:49:18 24 4
gpt4 key购买 nike

我正在使用以下加密方法来加密我的 MySQL 数据/字符串,如下所示:

$string = $row['reference'];
$secret_key = "PlowFish";
// Create the initialization vector for added security.
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
// Encrypt $string
$encrypted_string = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secret_key, $string, MCRYPT_MODE_CBC, $iv);

然后我通过超链接传递加密的字符串,如下所示:

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

然后我尝试解密字符串并将原始值回显到下一页,如下所示:

$enc = $_GET['ns_request'];
$secret_key = "PlowFish";
// Create the initialization vector for added security.
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$decrypted_string = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $secret_key, $enc, MCRYPT_MODE_CBC, $iv);

Your reference is: <?php echo $decrypted_string; ?>

但是这不起作用,因为它仍然回显加密值而不是解密值。请有人告诉我我要去哪里错了。谢谢

最佳答案

老实说,我没有测试上面的加密/解密,现在我可以看到它仍然是垃圾。如果需要,可以使用这里的小类。

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

像这样使用它:-

$enc=new encryption( array( 'key'=>'PlowFish' ) );
$encrypted_string = $enc->encrypt( $string );
echo $encrypted_string.BR;
$decrypted_string=$enc->decrypt( $encrypted_string );
echo $decrypted_string.BR;

关于php - 加密mysql数据并通过超链接,然后在下一页解密?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28603711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com