![](/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.