问题:在phpstorm中用AES产生的一段加密代码,发现解密的过程中字符串的长度和编码方式都不一样了。代码如下:
header("Content-type:text/html;charset=utf-8"); $secret1 = "rZa9GneIzd9MNyoTv/Ghpk2V6ZHD0KeQC7t0ymEH7cI="; $secret2 = 'rZa9GneIzd9MNyoTv/Ghpk2V6ZHD0KeQC7t0ymEH7cI='; $secret3 = 'rZa9GneIzd9MNyoTv/Ghpk2V6ZHD0KeQC7t0ymEH7cI='; var_dump(trim($secret1)); echo "<br>"; var_dump(trim($secret2)); echo "<br>"; var_dump($secret3); echo "<br>"; echo mb_detect_encoding($secret1, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5")); echo "<br>"; echo mb_detect_encoding($secret2, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5")); echo "<br>"; echo mb_detect_encoding($secret3, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5"));
通过CSDN问答,知道BOM问题,解决方案如下:
function removeBom($string)//去除BOM { if (substr($string, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) { return substr($string, 3); } return $string; } function ConvertUTF8ToGB2312($str)//转化为统一编码; { return iconv("UTF-8", "GB2312//IGNORE", $str); }