I think you are writing over unallocated memory. I don't think RSA_print()
allocates memory for BIO
, so you are passing an uninitialized pointer to it.
EDIT: As @jww pointed out in the comment, the BIO
object should be instantiated with a set of dedicated options. I edited the answer accordingly. I also changed the buffer size passed to 1024
.
Allocate space:
BIO * keybio = BIO_new(BIO_s_mem());
RSA_print(keybio, rsa, 0);
char buffer [1024];
std::string res = "";
while (BIO_read (keybio, buffer, 1024) > 0)
{
std::cout << buffer;
}
BIO_free(keybio); //appropriate free "method"
BIO_s_mem()
creates a memory BIO
method function. And BIO_new()
creates and initializes the object itself, using the supplied method.
Also please note that this code is c
-style. There is a nice way of wrapping pure pointer into c++
smart pointers, shown here: How to properly print RSA* as string in C++?.