Decoding Issue

Good day all,
I ran this code online
echo urldecode(base64_decode(‘cGFwYWRhbW15QHlhaG9vLmNvbQ%3D%3D’));

and it gave me this
papadammy@yahoo.com
��

The last two characters are not supposed to be part of the answer, what am I doing wrong?
Thanks in advance for your reply.

Remove the last 2

%3D%3D

And you should be fine.

Thanks spaceshiptrooper, it worked. The challenge now is that if I encode papadammy@yahoo.com, it will produce “cGFwYWRhbW15QHlhaG9vLmNvbQ%3D%3D”. I need this encoded thing to work without any manual interference for password recovery system. Any other idea?

I’ve tested a couple of emails and I discovered this problem is common with yahoo mail. I think the solution will be to find a way of removing %3D from any decoded variable having such.

Why not try this:

$str = str_replace('%3D', '', 'papadammy@yahoo.com');
echo urlencode(base64_encode($str));

Not tested, but does str_replace work with padding"

I just tested the code and it works…

encoded: cGFwYWRhbW15QHlhaG9vLmNvbQ%3D%3D
decoded: papadammy@yahoo.com

I made a slight issue with what I put above so the actual code would be:

<?php

$str = 'papadammy@yahoo.com';

$enc = urlencode(base64_encode($str));
echo "encoded: " . $enc;

$enc = str_replace('%3D', '',$enc);

$dec = urldecode(base64_decode($enc));
echo"<br />decoded: " . $dec;

The above will output the encoded string and then the decoded string working fine, just go from there to get the exact result you want

2 Likes

If you base64_encode first and then urlencode second (which is the order they have to be in to get that string to be decoded in the first place) then to decode the string you need to urldecode first and base64_decode second

echo base64_decode(urldecode('cGFwYWRhbW15QHlhaG9vLmNvbQ%3D%3D'));

Then the encoded %#D%3D will be converted back to == first and then discarded automatically.

The way round that you have it you are trying to base64 decode a string that isn’t all base64

1 Like

To everyone of you who took time to educate me on this, I am really grateful. Thank you all.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.