"b64" is a filter BIO, it won't hold on to your data. You need to append a "memory BIO" to the back end of the filter bio so that your output can be accumulated.

There are samples on how to do this in the OpenSSL book as well as a rather lengthy discussion on BIO's in general.

Also "BIO_get_mem_ptr()" gives you a pointer to BUF_MEM structure, not a char*. Your compiler should have yelled at you for that.


On Oct 13, 2005, at 12:41 PM, Adam Jones wrote:

Below is the code I am using to try and test the base64 encode in openssl. I am using rand to generate a binary and then encoding that to base64. Instead of using a file, I want to use memory to output the base64 encoded buffer. This code compiles and runs, but my output buffer is all 0. Any help would be appreciated. What have I missed?

The variable written does show 16 like it should......help!

#include <iostream>
#include <memory.h>
#include "evp.h"
#include "rand.h"
#include "bio.h"

using namespace std;

int main()
{
 BIO *b64;
 unsigned char *pbuffer = new unsigned char [16];
 unsigned char *pOutput = new unsigned char [100];
 int written;

 memset(pOutput, '0', 100);
 RAND_bytes(pbuffer, 16);
 b64 = BIO_new(BIO_f_base64());
 written = BIO_write(b64, pbuffer, 16);

 cout << written << endl;

 BIO_get_mem_ptr(b64, pOutput);

 for ( int nLoop = 0; nLoop< 16; nLoop++)
 {
 cout << pOutput[nLoop];
 }
 cout << "\n" << endl;

 BIO_free_all(b64);

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to