Base64 En-/Decoding using OpenSSL in C

IT, UNI Kommentare deaktiviert

Recently i had to implement Base64 en & decoding using openssl’s bio library, which by the way is just great, but a little “under-documented!”

If you ever wondered, why the standard example of decoding base64 data always returns 0 when using it with your test data? Well there is some nice undocumented feature: Strings that do not end with a newline ‘\n’ are not processed! So you have two possibilities: adding a newline to the string or use the following flag:

BIO_set_flags(BIO* to your bio_f_base64, BIO_FLAGS_BASE64_NO_NL);

Edit: 21/Dec/08:

After handin of the courses homework i can now give some details about how to do it:

Using a chain of BIO filters is the most flexible way to handle proper base64 en-/decoding:


//write base64 coded data to stdout
BIO* b64 = BIO_new(BIO_f_base64());
BIO* bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
bio_out = BIO_push(b64, bio_out); //attach output bio to base64 bio
BIO_write(bio_out,"data",sizeof("data"));
BIO_flush(bio_out); //flush the buffer
BIO_free_all(bio_out); //cleanup!

For more information see the super perfect documentation: http://www.openssl.org/docs/crypto/BIO_f_base64.html

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS