magicianiam
magicianiam

Reputation: 1579

PHP base64 decode returns garbage characters

I'm decoding a string that was encoded in Python.

When using an online simulator for the decoding: https://www.base64decode.org/

The correct value is presented but when I'm decoding it on my end using PHP base64_decode it returns garbage characters:

["(","bean_id","=��M�MM̋LY �KM X�KNX��KY��X��L؍ ��H�

I'm guessing this has something to do with my charset?

The encoded string:

WyIoIiwiYmVhbl9pZCIsIj0+IiwiMDAxN2E1NzItMWQ2NS00NWJhLTljNzEtZGRmNmFiMzkzYjQ0IiwiKSJd

When decoded using the online simulator results in this:

["(","bean_id","=>","0017a572-1d65-45ba-9c71-ddf6ab393b44",")"]

My code:

$page = $_GET['code'];
$plainText = base64_decode($page);

echo $plainText;

Additional information:

The problem occurs when I fetch the string from the url.

Upvotes: 4

Views: 3566

Answers (2)

Md. Khairullah Nayan
Md. Khairullah Nayan

Reputation: 439

How to remove ^ b" on base64 decode?

I got the best solutions by following

At first encode like following sequence

JSON Encode > URL Encode > Base64 Encode

Now you can decode safely by following sequence

Base64 Decode > URL Decode > JSON Decode

It will encode the special characters if exists. and you will not find any garbage like ^ b" ...

Upvotes: 0

magicianiam
magicianiam

Reputation: 1579

I was fetching the string in the URL using $_GET

Since it's in the URL the + will be transformed into a (Space)

That was causing the garbage character.

What I'm doing now to avoid this problem is to use urlencode on my string after $_GET so that the transformed characters will be returned to their original form.

Working late really has its downside. Thank you, everyone.

Upvotes: 5

Related Questions