![](/img/trans.png)
[英]Encrypt string when passing it through url as hyperlink and decrypt to echo out on next page?
[英]encrypt mysql data and pass through hyperlink, then decrypt on the next page?
我正在使用以下加密方法来加密我的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; ?>
但是,这不起作用,因为它仍然回显加密的值,而不是解密的值。 请有人能告诉我我要去哪里错了。 谢谢
您可能要先对数据进行base64_encode,然后再发送url,然后再进行base64_decode,然后再尝试使用mcrypt解密。 加密的文本包含不安全的字符,因此会被篡改。
$string = $row['reference'];
$secret_key = "PlowFish";
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$encrypted_string = base64_encode( mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secret_key, $string, MCRYPT_MODE_CBC, $iv) );
$enc = base64_decode( $_GET['ns_request'] );
$secret_key = "PlowFish";
$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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.