简体   繁体   中英

base64_decode omits one byte

I'm using PHP's base64_decode on this string:

Dg+to8RaC3VzAGRThP7iiXe0f5bgp7xmcQoqaAJxggs=

and it should return 32 bytes of data. But somehow I end up with a string thats only 31 bytes long.

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. When I do this: $len = strlen($data); then $len will contain 31 characters instead of 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)

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.

EDIT 2: additional tests yield this: 1. when I embedd the base64 strings directly in the PHP file it always works. 2. when I get the base64 string from a $_GET variable then it sometimes doesn't work.

is $_GET broken?

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 + .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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