openssl crypto library - base64 conversion
Posted
by Hassan Syed
on Stack Overflow
See other posts from Stack Overflow
or by Hassan Syed
Published on 2010-04-01T12:11:17Z
Indexed on
2010/04/01
12:13 UTC
Read the original article
Hit count: 288
I'm using openssl BIO objects to convert a binary string into a base64 string. The code is as follows:
void ToBase64(std::string & s_in) {
BIO * b_s = BIO_new( BIO_s_mem() );
BIO * b64_f = BIO_new( BIO_f_base64() );
b_s = BIO_push( b64_f , b_s);
std::cout << "IN::" << s_in.length();
BIO_write(b_s, s_in.c_str(), s_in.length());
char * pp;
int sz = BIO_get_mem_data(b_s, &pp);
std::cout << "OUT::" << sz << endl;
s_in.assign(pp,sz);
//std::cout << sz << " " << std::string(pp,sz) << std::endl;
BIO_free (b64_f); // TODO ret error potential
BIO_free (b_s); //
}
The in length is either 64 or 72. However the output is always 65, which is incorrect it should be much larger than that. The documentation isn't the best in the world, AFAIK the bio_s_mem object is supposed to grow dynamically. What am I doing wrong ?
© Stack Overflow or respective owner