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.

    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了