> From: owner-openssl-us...@openssl.org On Behalf Of Craig > Sent: Monday, 21 November, 2011 11:56
> I'm fairly new to C/C++ so please bear with me. Warning: beware of people who tell you C++ is a superset of C. It's not. C++, deliberately, includes MOST of C -- maybe 80-90%, depending how you count -- but NOT all. So in some cases it matters which you are using. > I'm having issues base64 decoding files that have been > encrypted using AES-256-CBC and the base64 encoded. I'm > using the following code to base64 decode and this works > fine on "plain text" files that have been base64 encoded. > char * base64Decode(unsigned char * input, int length) { > BIO *b64, *bmem; > char * buffer = (char *) malloc(length); > memset(buffer, 0, length); > b64 = BIO_new(BIO_f_base64()); > bmem = BIO_new_mem_buf(input, length); > bmem = BIO_push(b64, bmem); > BIO_read(bmem, buffer, length); > BIO_free_all(bmem); > return buffer; > } Nit: the cast on malloc is not needed in correct C (if your compiler complains, you didn't #include <stdlib.h> and just throwing in a cast is NOT the correct solution); in C++ it is better to use 'new' and 'delete' instead (which don't need any casts). Note that a base64 decoded result will ALWAYS be smaller than the input, so malloc'ing for length is more than you actually need. However, the computation of exactly what you need can be a little tricky, and usually it's better to err on the side of too much than too little. > The returned char is always much smaller than expeted for > files that have been encrypted. I do pass the correct encoded > data size along with the data What makes you think it's smaller? Are you trying to treat the decoded but still encrypted data as a C string? It's not. It's raw binary data that usually and in your case contains 'null' characters (byte value zero) which terminate a string. With modern (>WW2) ciphers even unencrypted/clear data CAN be any binary data, although you may choose in your application(s) to encrypt only C strings, or only printable ones. The return value from BIO_read is the number of bytes read, which for BIO_f_base64 is the number of bytes decoded, or negative for error. Check and use that. If you are using C++, you CAN put binary data in a std::string, which is NOT the same as a C OR C++ char* string. There is no standard or builtin way, in either language, to return a pointer (char*) plus count together. You can: - pass the (already-allocated) buffer and return the length - return the newly-allocated pointer and store the length "to" an argument (that is, through a pointer argument) - return the length and store the newly-allocated pointer "to" an argument - return an error code/status, or nothing, and store both length and newly-allocated pointer "to" arguments - return a struct you declare containing pointer and length - store "to" an argument struct ditto - more complicated possibilities <snip rest> ______________________________________________________________________ OpenSSL Project http://www.openssl.org User Support Mailing List openssl-users@openssl.org Automated List Manager majord...@openssl.org