abduct
abduct

Reputation: 25

Post data missing characters in base64

I am trying to send a post to my php script from a C# application but the base64_decode() keeps failing.

This is the string my post is trying to send:

stWC3F34AboxsfjbxAWtGU8WUHIombDMd6Z4WXxyOae0+NlISFHVEATxjE8EFKKMkDnQe0oFu8AvQM9lRaSSMgjYnyZvhXB8EiQDImgrsTmgvwsIeu4qZiW2O6cK4oaPUJIIFnEDlNCstc4s1mzseq0zv0xYPy3tRqbWPXVV9jv9BZiMFGxeVD+OKV/mO0NkVP4UVUMcFOXVCKh5HslVZv59lEqa5C230HSyv17/W0G1e7q4tBxWEhUwCfbldNtk

and when the script receives it the "+" are replaced with " " so i use str_replace() to readd them and then pass the output to base64_decode() but it keeps returning false.

This is the string that gets passed to base64_decode() BEFORE replacing the " " with "+"

stWC3F34AboxsfjbxAWtGU8WUHIombDMd6Z4WXxyOae0 NlISFHVEATxjE8EFKKMkDnQe0oFu8AvQM9lRaSSMgjYnyZvhXB8EiQDImgrsTmgvwsIeu4qZiW2O6cK4oaPUJIIFnEDlNCstc4s1mzseq0zv0xYPy3tRqbWPXVV9jv9BZiMFGxeVD OKV/mO0NkVP4UVUMcFOXVCKh5HslVZv59lEqa5C230HSyv17/W0G1e7q4tBxWEhUwCfbldNtk

This is the string that gets passed to base64_decode() AFTER replacing the " " with "+"

stWC3F34AboxsfjbxAWtGU8WUHIombDMd6Z4WXxyOae0+NlISFHVEATxjE8EFKKMkDnQe0oFu8AvQM9lRaSSMgjYnyZvhXB8EiQDImgrsTmgvwsIeu4qZiW2O6cK4oaPUJIIFnEDlNCstc4s1mzseq0zv0xYPy3tRqbWPXVV9jv9BZiMFGxeVD+OKV/mO0NkVP4UVUMcFOXVCKh5HslVZv59lEqa5C230HSyv17/W0G1e7q4tBxWEhUwCfbldNtk

And from what I read the only reason why it should return false is if the string contains characters from outside the base64 alphabet, but it appears to be a valid base64 string.

Any help is appreciated. If I can't get this fixed I'll just start posting hex strings instead of base64.

Upvotes: 1

Views: 3864

Answers (3)

Jim Mischel
Jim Mischel

Reputation: 134045

You also need to replace any slash characters (/), which represents the value 63 in standard base64 encoding. The common usage is to convert + to -, and / to _.

See http://en.wikipedia.org/wiki/Base64#URL_applications

In addition, some base64 decoders require that the string be padded with = characters so that the string's length is a multiple of 3. I don't know if PHP's base64_decode requires this. See the linked Wikipedia article for information about the padding.

Upvotes: 2

Ortiga
Ortiga

Reputation: 8824

Try using urlencode in php and HttpServerUtility.UrlDecode in C#

EDIT: Sorry, what you want is the other way round:

HttpServerUtility.UrlEncode in c# and urldecode in php

Upvotes: 1

Smetad Anarkist
Smetad Anarkist

Reputation: 898

Are you doing a URLEncode on the string before you send it? That should fix the problem with the plus sign.

Upvotes: 2

Related Questions