PHP 打包/解包错误
我必须将旧的“加密”数据从旧系统转换为正确的加密算法。我有这样的代码:
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 Is A
而不是 StackOverflow Is AWESOME
I have to convert an old "encrypted" data to a proper encryption algorithm from an old system. I have this code:
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";
Why the output shows StackOverflow Is A
and not StackOverflow Is AWESOME
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Base64 编码将字符串的大小扩展了约 33%。您传递的是原始字符串的长度,而不是 Base64 编码的字符串:
因此您要砍掉 8 个字符,留下
解码为
base64 encoding expands the size of a string by about 33%. You're passing in the length of the ORIGINAL string, not the base64 encoded one:
So you're chopping off 8 characters, leaving you with
which decodes to