views:

383

answers:

1

I'm trying to decode base64-encoded string with openssl. However, it works only 4 times out of 5.

Decoded string should always be 64 chars long. BIO_read() always returns 64. However, sometimes returned buffer is shorter than 64!

Any ideas what is wrong? How can i always get the correct string?

+4  A: 

Are you using str[n]cpy? You can't! Base64 encoded data can contain null characters, which C string processing functions interpret as end-of-string.

Use memcpy instead of str[n]cpy, memcmp instead of strcmp, etc. These functions require you to know your data size, but I believe that you do know it.

Also if you're not very confident about C-style strings and such, there's plenty of information to be found about the topic here.

Artelius
Thanks!!! That was the missing part of the puzzle.
Mantas
Hmmm, isn't base64 encoded data composed entirely of printable characters (a-z, A-Z, 0-9, '+' and '/' [and '=' for padding])?
pmg
I'm referring to the data that was encoded as Base64 (i.e. what you get back when you Base64 decode), not the Base64 encoded form of the data.
Artelius