dqmdlo9674 2013-11-12 22:25 采纳率: 100%
浏览 71

PHP:ASCII和UTF-8字符串比较

I have following test.php script:

$AES_IV = "4epZqMl8BQukhip6WQjQHg==";
$AES_KEY = "zvKmer0cPiJdQQ1RPjQOCF/wURt+31UdingRoPj4+Yc=";

$guid = $_GET["guid"];
$encryptedGuid = urldecode($_GET["encryptedGuid"]);

$decryptedGuid = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, base64_decode($AES_KEY), base64_decode($encryptedGuid), MCRYPT_MODE_CBC, base64_decode($AES_IV));

echo "Guid is: ".$guid."</br>";
echo "Decrypted GUID is: ".$decryptedGuid."</br>";

if ($decryptedGuid === $guid)
{
    echo "Guid and decrypted Guid are equals.";
}
else 
{
    echo "Guid and decrypted Guid are not equals.";
}

I call this script as follows:

http://localhost:8087/test/test.php?guid=08201E62-DFA5-8A50-ACFC-A811871804CD&encryptedGuid=ED40y72BJvzXmN0Aj9eb85VdIJKu6LUYQuYSWMPOhIQcm5SIquZAhRMmhsC7ax2e

Test.php script should display "Guid and decrypted Guid are equals." but it never occurs. I tried to check encoding of strings and $guid is ASCII and $encryptedGuid is UTF-8. Can you please help me how to solve this problem? I tried to convert ASCII to UTF-8 but I wasn't successful because result of e.g. iconv function was again ASCII. I think convert ASCII to UTF-8 can be solution but I am not sure if it is always true that $_GET returns ASCII.

Thank you very much for your help.

  • 写回答

1条回答 默认 最新

  • douhuan2101 2016-11-28 10:40
    关注

    I had a similar issue and I was able to work it out thanks to Dennis' comment about padding.

    After I decrypted my AES string and I noticed that one particular param in the URL wasn't quite right...

    E.g. test=1&foo=bar
    

    It looked fine. However, when I was trying to do a string comparison with the final value in the URL (bar) it would never be true.

    if ($foo == 'bar')
    

    So, I compared the strings using bin2hex there was some sort of repeated string added to the end of the param value (0505050505). What I didn't realise was that this was padding added to the string by the encryption.

    For some reason rtrim didn't help me so what I did was added a useless param to the end of my URL before encryption (e.g. padding=1). This made sure that the last of my "important" params didn't have the padding added to the string.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测