PHP打包/解包错误
发布时间:2020-12-13 22:02:52 所属栏目:PHP教程 来源:网络整理
导读:我必须将旧的“加密”数据转换为旧系统的正确加密算法.我有这个代码: function unpackString($s,$l){ $tmp=unpack('c'.$l,$s); $return=NULL; foreach($tmp as $v){ if($v0){ $return.=chr($v); } } return $return; } function packString($s,$l){ $return
我必须将旧的“加密”数据转换为旧系统的正确加密算法.我有这个代码:
function unpackString($s,$l){ $tmp=unpack('c'.$l,$s); $return=NULL; foreach($tmp as $v){ if($v>0){ $return.=chr($v); } } return $return; } function packString($s,$l){ $return=NULL; for($i=0;$i<$l;$i++){ $return.=pack('c',ord(substr($s,$i,1))); } return $return; } $string='StackOverflow Is AWESOME'; $l=strlen($string); $encoded=packString(base64_encode($string),$l); $decoded=base64_decode(unpackString($encoded,$l)); echo "n".$decoded."n"; 为什么输出显示StackOverflow是A而不是StackOverflow很棒 解决方法
base64编码将字符串的大小扩展了大约33%.你传递的是ORIGINAL字符串的长度,而不是base64编码的字符串:
StackOverflow Is AWESOME - 24 chars plaintext U3RhY2tPdmVyZmxvdyBJcyBBV0VTT01F - 32 chars base64 encoded 所以你要砍掉8个字符,留下你 U3RhY2tPdmVyZmxvdyBJcyBB 它解码为 StackOverflow Is A (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |