简体   繁体   English

base64_decode省略一个字节

[英]base64_decode omits one byte

I'm using PHP's base64_decode on this string: 我在这个字符串上使用PHP的base64_decode

Dg+to8RaC3VzAGRThP7iiXe0f5bgp7xmcQoqaAJxggs=

and it should return 32 bytes of data. 它应该返回32个字节的数据。 But somehow I end up with a string thats only 31 bytes long. 但不知何故,我最终得到的字符串只有31个字节长。

This is what I'm doing: 这就是我正在做的事情:

$data = base64_decode($data);
if($data == FALSE)
{
    die("decode faild");
}

The problem is that the decode will actually work. 问题是解码实际上会起作用。 But it returns only 31 bytes. 但它只返回31个字节。 When I do this: $len = strlen($data); 当我这样做: $len = strlen($data); then $len will contain 31 characters instead of 32. 那么$len将包含31个字符而不是32个字符。

With the following string it woks (strangely): 使用以下字符串(奇怪地):

az8XFgxw/ODAr3EDElvgab/axINKVMDCrw5u51gn6bo=

I already tried urlencode after the base64 and urldecode before decoding but the result was exactly the same (It worked but omited one byte) 在解码之前我已经在base64urldecode之后尝试了urlencode但是结果完全相同(它工作但省略了一个字节)

What am I missing? 我错过了什么?


EDIT: I printed out all bytes. 编辑:我打印出所有字节。 It REALLY omits the last byte! 它真的省略了最后一个字节! 0x0B (the last byte) is missing but the other bytes are there. 缺少0x0B (最后一个字节),但其他字节在那里。

EDIT 2: additional tests yield this: 1. when I embedd the base64 strings directly in the PHP file it always works. 编辑2:额外的测试产生这个:1。当我直接在PHP文件中嵌入base64字符串时,它始终有效。 2. when I get the base64 string from a $_GET variable then it sometimes doesn't work. 2.当我从$_GET变量中获取base64字符串时,它有时不起作用。

is $_GET broken? $_GET坏了吗?

It's the + my friend, it's being interpreted as 这是+我的朋友,它被解释为 (space). (空间)。 So either url_encode() before appending the data to the URL and then url_decode() in your script or simply replace the space with + . 因此,在将数据附加到URL之前使用url_encode() ,然后在脚本中添加url_decode() ,或者只是用+替换空格。

$data = base64_decode(str_replace(' ', '+', $data));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM