简体   繁体   中英

C++ base64 encoding - PHP compatible

I have a desktop app that creates and posts about 2-3k of binary data to a web server. I have worked out the posting piece using HttpSendRequest , which works great with text, but obviously fails with binary. From my research it looks like I need to base64 encode the data from my c++ app, and then use base64_decod e in PHP to store this data.

I have downloaded several c/c++ files designed to encode in base64 , but either they simply don't work, or the encoding isn't compatible with the PHP encoding.

I guess my questions are this:

  1. Am I going about this the right way? Is there a better format to transport this data?
  2. Is anyone aware of a simple class or other c++ code that can help me encode this data in the proper format?

-- UPDATE -- I have been playing with this for the past day and am now more confused than ever. Here's the data:

  1. Original binary data: http://pastebin.com/cPWjGN3q
  2. After base64 encoding in C++: http://pastebin.com/H4Jze96g
  3. Uploaded as encoded in php: http://pastebin.com/MRLZrsve
  4. Decoded in php: http://pastebin.com/TJs5WCNj
  5. The code I'm using to POST the data: http://pastebin.com/L856rKmw .

I'm not sure where things are going wrong, but any help getting this straightened out will be greatly appreciated!

-- UPDATE 2 -- The POST process changes the + characters in the encoded data to spaces. See the following:

Data as POSTED from C++: http://pastebin.com/NiSE7GUe

As copied via FTP to server: http://pastebin.com/S0qjfreM

Is there a logical reason why the post process would change this character? I would prefer to understand and resolve the actual problem, but perhaps performing a string replace would be sufficient to finally move past this problem?

Base64 is a well-defined encoding mechanism - so there shouldn't be inconsistencies between implementations. If you're seeing consistent failures using different implementations then the problem is somewhere other than the encoding algorithm.

You can test this by simply dumping the output to the file (or even the console).

Indeed, while the encoding method is relatively robust, there are some characters which can have special meaning depending on the context in which they appear (URL, POST, Html). Add to that the fact that HTTP, and most http clients implements different transfer and content encodings can cause additional confusion.

It might help if we saw what code you are using to prep the data and invoke HttpSendRequest

You could use libb64 to base64 encode your input, however base64 encoding is fairly standard, so I suspect that you may be doing something wrong when you say other libraries produce encodings that aren't compatible with PHP.

Have you tried writing the base64 encoded data into a file, then reading that in with PHP? That way you can at least eliminate the (seemingly unlikely) chance of encoding incompatibilities.

EDIT:

From the extra information you've posted, it looks like you may not be properly URL encoding the base64 data before sending it in to HttpSendRequest. Base64, though text based, still uses some characters that have a different meaning in url encoding,

Have a read of this for an explanation of the encoding. I suspect that is why the +'s in your base 64 string are missing when they arrive to PHP, as a + in URL encoding actually means space.

So, you should try URL encoding the string before sending to HttpSendRequest. I can't name any APIs to do it off the top of my head, but it should be simple enough to write one that takes care of all the base64 characters. At the very least you'll need to replace the instances of + with %2B

It seems like PHP does the de-coding for you, so it should only be neccesary to encode before sending, rather than decoding when you receive.

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