I\'ve been trying to figure out the openssl documentation for base64 decoding and encoding. I found some code snippets below
This works for me, and verified no memory leaks with valgrind.
#include
#include
#include
#include
#include
#include
#include
namespace {
struct BIOFreeAll { void operator()(BIO* p) { BIO_free_all(p); } };
}
std::string Base64Encode(const std::vector& binary)
{
std::unique_ptr b64(BIO_new(BIO_f_base64()));
BIO_set_flags(b64.get(), BIO_FLAGS_BASE64_NO_NL);
BIO* sink = BIO_new(BIO_s_mem());
BIO_push(b64.get(), sink);
BIO_write(b64.get(), binary.data(), binary.size());
BIO_flush(b64.get());
const char* encoded;
const long len = BIO_get_mem_data(sink, &encoded);
return std::string(encoded, len);
}
// Assumes no newlines or extra characters in encoded string
std::vector Base64Decode(const char* encoded)
{
std::unique_ptr b64(BIO_new(BIO_f_base64()));
BIO_set_flags(b64.get(), BIO_FLAGS_BASE64_NO_NL);
BIO* source = BIO_new_mem_buf(encoded, -1); // read-only source
BIO_push(b64.get(), source);
const int maxlen = strlen(encoded) / 4 * 3 + 1;
std::vector decoded(maxlen);
const int len = BIO_read(b64.get(), decoded.data(), maxlen);
decoded.resize(len);
return decoded;
}
int main()
{
const char* msg = "hello";
const std::vector binary(msg, msg+strlen(msg));
const std::string encoded = Base64Encode(binary);
std::cout << "encoded = " << encoded << std::endl;
const std::vector decoded = Base64Decode(encoded.c_str());
std::cout << "decoded = ";
for (unsigned char c : decoded) std::cout << c;
std::cout << std::endl;
return 0;
}
Compile:
g++ -lcrypto main.cc
Output:
encoded = aGVsbG8=
decoded = hello