比较相同的字符串 returns false

comparing identical strings returns false

我有 2 table 个商家 table 和订单 table。每个 table 都有一个密钥,我用它来比较以检查安全性。两者都是加密的。我解密它们以进行验证。问题是解密后我得到相同的字符串值,但比较它们 returns false。这是代码

如果字符串具有特殊字符,则会出现此问题;如果字符串仅包含字母和数字,则不会出现此问题

public function merchant_encrypt($pure_string, $encryption_key) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv);
    return $encrypted_string;
}

public function merchant_decrypt($encrypted_string, $encryption_key) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH, $encryption_key, $encrypted_string, MCRYPT_MODE_ECB, $iv);
    return $decrypted_string;
}

public function replace_spechial_charater($value){
    $value = str_replace('+=','plusequal',$value);
    $value = str_replace('=','equalsign',$value);
    $value = str_replace('+','plussign',$value);
    $value = str_replace('/','slashsign',$value);
    $value = str_replace('&','andsign',$value);
    return $value;
}

public function restore_spechial_charater($value){
    $value = str_replace('plusequal','+=',$value);
    $value = str_replace('equalsign','=',$value);
    $value = str_replace('plussign','+',$value);
    $value = str_replace('slashsign','/',$value);
    $value = str_replace('andsign','&',$value);
    return $value;
}

public function strhex($string) { 
    $hexstr = unpack('H*', $string); 
    return array_shift($hexstr); 
}

正在保存商家密钥

$enc_key = $row['merchant_id'];
$merchant_key = trim($_POST['key']); //e.g: 1234abcd+=&$
$merchant_key = replace_spechial_charater($merchant_key);
$encrypted_key = merchant_encrypt($merchant_key ,$enc_key);
$encrypted_key = base64_encode($encrypted_key);
//save $encrypted_key in the merchant table

解密

$decrypted_key = base64_decode($row['key']);
$decrypted_key = decrypt($decrypted_key,$row['merchant_id']);
$decrypted_key = restore_spechial_charater($decrypted_key);
// the result is 2d1d54rt5h4th5rh5tr1h%$&^/+=gdgdfgd

相同的未加密密钥在商家网站中被加密,但使用另一个 enc_key

$enc_key = $row['order_id'];
$merchant_key = $row['key']; // 1234abcd+=&$
$merchant_key = replace_spechial_charater($merchant_key);
$encrypted_key = merchant_encrypt($merchant_key ,$enc_key);
$encrypted_key = base64_encode($encrypted_key);
//send $encrypted_key with other parameters to the payment gateway then returned to the php script

$order_decrypted_key = base64_decode($row['order_id']);
$order_decrypted_key = decrypt($order_decrypted_key ,$row['order_id']);
$order_decrypted_key = restore_spechial_charater($order_decrypted_key );
// the result is 2d1d54rt5h4th5rh5tr1h%$&^/+=gdgdfgd

var_dump(strip_tags($decrypted_key));
var_dump(strip_tags($order_decrypted_key));
$result = strcasecmp( trim($decrypted_key), trim($order_decrypted_key) );
echo $result;
//var_dump(trim()) returns the same result for both values

结果是:

string(39) "2d1d54rt5h4th5rh5tr1h%$&^/+=gdgdfgd" 
string(35) "2d1d54rt5h4th5rh5tr1h%$&^/+=gdgdfgd" 
3

然后:

$order_key = strhex($order_decrypted_key);
$merchant_key = strhex($decrypted_key);

var_dump(trim($decrypted_key));
var_dump(trim($order_decrypted_key));

字符串(78)

"326431643534727435683474683572683574723168252426616d703b5e2f2b3d67646764666764"

字符串(70)

"3264316435347274356834746835726835747231682524265e2f2b3d67646764666764"

那么如何解决这个问题并检查它们是否相等

你们两个字符串不一样,大家聚齐了。恐怕没有神秘的填充物。

您的较长字符串是 2d1d54rt5h4th5rh5tr1h%$&^/+=gdgdfgd (注意中间的 html 实体,占 4 个额外字符)。

(通过 运行 在您的解压数据中找到以下内容):

$string = "326431643534727435683474683572683574723168252426616d703b5e2f2b3d67646764666764";
$packed = pack("H*", $string);
var_dump($packed);

回复:

string(39) "2d1d54rt5h4th5rh5tr1h%$&^/+=gdgdfgd"

我只能猜测您是在网络上而不是在终端上打印比较结果,并且您呈现的是浏览器呈现的结果(而不是实际结果),因此看不到那额外的四个字符。

在保存数据之前,您可能应该 运行 一些东西来解码输入中的 html 实体。或者你可以在比较之前做,但更好地保存数据。

$decrypted_key       = htmlspecialchars_decode($decrypted_key);
$order_decrypted_key = htmlspecialchars_decode($order_decrypted_key);