我正在使用以下加密方法来加密我的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; ?>
然而这不起作用,因为它仍然是加密值而不是解密值。请有人告诉我我哪里出错了。谢谢
答案 0 :(得分:0)
您可能希望在发送网址之前对数据进行base64_encode,然后在尝试使用mcrypt进行解密之前使用base64_decode。加密文本包含不是url安全的字符,因此会被破坏。
$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; ?>
答案 1 :(得分:0)
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;