Here's the one I'm using:

#include <stdint.h>
#include <stdlib.h>


static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                                'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                                'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
                                'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                                'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                                'w', 'x', 'y', 'z', '0', '1', '2', '3',
                                '4', '5', '6', '7', '8', '9', '+', '/'};
static char *decoding_table = NULL;
static int mod_table[] = {0, 2, 1};


char *base64_encode(const unsigned char *data,
                    size_t input_length,
                    size_t *output_length) {

    *output_length = 4 * ((input_length + 2) / 3);

    char *encoded_data = malloc(*output_length);
    if (encoded_data == NULL) return NULL;

    for (int i = 0, j = 0; i < input_length;) {

        uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
        uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
        uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;

        uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;

        encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
    }

    for (int i = 0; i < mod_table[input_length % 3]; i++)
        encoded_data[*output_length - 1 - i] = '=';

    return encoded_data;
}


unsigned char *base64_decode(const char *data,
                             size_t input_length,
                             size_t *output_length) {

    if (decoding_table == NULL) build_decoding_table();

    if (input_length % 4 != 0) return NULL;

    *output_length = input_length / 4 * 3;
    if (data[input_length - 1] == '=') (*output_length)--;
    if (data[input_length - 2] == '=') (*output_length)--;

    unsigned char *decoded_data = malloc(*output_length);
    if (decoded_data == NULL) return NULL;

    for (int i = 0, j = 0; i < input_length;) {

        uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];

        uint32_t triple = (sextet_a << 3 * 6)
        + (sextet_b << 2 * 6)
        + (sextet_c << 1 * 6)
        + (sextet_d << 0 * 6);

        if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
        if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
        if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
    }

    return decoded_data;
}


void build_decoding_table() {

    decoding_table = malloc(256);

    for (int i = 0; i < 64; i++)
        decoding_table[(unsigned char) encoding_table[i]] = i;
}


void base64_cleanup() {
    free(decoding_table);
}

Keep in mind that this doesn't do any error-checking while decoding - non base 64 encoded data will get processed.

Answer from ryyst on Stack Overflow
Author: elzoughby
🌐
GitHub
github.com › joedf › base64.c
GitHub - joedf/base64.c: Base64 Library in C · GitHub
Base64 Library in C. Contribute to joedf/base64.c development by creating an account on GitHub.
Starred by 76 users
Forked by 17 users
Languages: C 93.0% | Batchfile 3.5% | Shell 3.5%
Discussions

I created a base64 library
I think you've taken things a bit too literally. You are copying the entire input into an array 32 times its size and extracting one bit of the input into an entire integer's size. This is wildly inefficient and completely unnecessary. All you need to do is take the input 3 bytes at a time, which gives you 24 bits of input data. From that 24 bits of input data you can decode 4 characters of output. If you are outputting to a stream then no allocations need to be made, or if encoding in memory then a single buffer for output is needed. #include #include #include #include size_t b64_size(size_t size) { return size / 3 * 4 + (size % 3 ? 4 : 0); } char *b64_encode(char *in, size_t in_size) { const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; size_t out_size = b64_size(in_size) + 1; char *out = calloc(1, out_size + 1); size_t out_cursor = 0; for (size_t in_cursor = 0; in_cursor < in_size;) { uint32_t triplet = 0; uint32_t triplet_mask = 0; for (int i = 0; i < 3; i++, in_cursor++) { triplet <<= 8; triplet_mask <<= 8; if (in_cursor < in_size) { triplet |= in[in_cursor]; triplet_mask |= 0xFF; } } for (int i = 0; i < 4; i++) { if (triplet_mask & 0xFC0000) out[out_cursor++] = b64[(triplet & 0xFC0000) >> 18]; else out[out_cursor++] = '='; triplet <<= 6; triplet_mask <<= 6; } } return out; } int main(int argc, char *argv[]) { char *test_b64 = b64_encode(argv[1], strlen(argv[1])); printf("%s\n", test_b64); free(test_b64); } More on reddit.com
🌐 r/C_Programming
24
49
December 28, 2024
Wrote a simple base64 encoder/decoder and thought I'd share it.
glib is particularly complicated. You could also use e.g. the libb64 . Your code is good, but I recommend you to use a separate length argument as base64 allows for the encoding of NUL bytes. Your code is a bit strange in that it constantly reallocates buffers, which is a bit slow and unexpected. I'm not sure how to use your code as you do not tell the user anywhere how long the allocated data region is. More on reddit.com
🌐 r/C_Programming
14
7
March 31, 2017
🌐
SourceForge
libb64.sourceforge.net
libb64: Base64 Encoding/Decoding Routines
libb64 is a library of ANSI C routines for fast encoding/decoding data into and from a base64-encoded format.
🌐
Reddit
reddit.com › r/c_programming › i created a base64 library
r/C_Programming on Reddit: I created a base64 library
December 28, 2024 -

Hi guys, hope you're well and that you're having a good christmas and new year,

i created a library to encode and decode a sequence for fun i hope you'll enjoy and help me, the code is on my github:

https://github.com/ZbrDeev/base64.c

I wish you a wonderful end-of-year holiday.

Top answer
1 of 5
45
I think you've taken things a bit too literally. You are copying the entire input into an array 32 times its size and extracting one bit of the input into an entire integer's size. This is wildly inefficient and completely unnecessary. All you need to do is take the input 3 bytes at a time, which gives you 24 bits of input data. From that 24 bits of input data you can decode 4 characters of output. If you are outputting to a stream then no allocations need to be made, or if encoding in memory then a single buffer for output is needed. #include #include #include #include size_t b64_size(size_t size) { return size / 3 * 4 + (size % 3 ? 4 : 0); } char *b64_encode(char *in, size_t in_size) { const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; size_t out_size = b64_size(in_size) + 1; char *out = calloc(1, out_size + 1); size_t out_cursor = 0; for (size_t in_cursor = 0; in_cursor < in_size;) { uint32_t triplet = 0; uint32_t triplet_mask = 0; for (int i = 0; i < 3; i++, in_cursor++) { triplet <<= 8; triplet_mask <<= 8; if (in_cursor < in_size) { triplet |= in[in_cursor]; triplet_mask |= 0xFF; } } for (int i = 0; i < 4; i++) { if (triplet_mask & 0xFC0000) out[out_cursor++] = b64[(triplet & 0xFC0000) >> 18]; else out[out_cursor++] = '='; triplet <<= 6; triplet_mask <<= 6; } } return out; } int main(int argc, char *argv[]) { char *test_b64 = b64_encode(argv[1], strlen(argv[1])); printf("%s\n", test_b64); free(test_b64); }
2 of 5
8
Based (Pun intended)
Top answer
1 of 16
138

Here's the one I'm using:

#include <stdint.h>
#include <stdlib.h>


static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                                'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                                'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
                                'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                                'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                                'w', 'x', 'y', 'z', '0', '1', '2', '3',
                                '4', '5', '6', '7', '8', '9', '+', '/'};
static char *decoding_table = NULL;
static int mod_table[] = {0, 2, 1};


char *base64_encode(const unsigned char *data,
                    size_t input_length,
                    size_t *output_length) {

    *output_length = 4 * ((input_length + 2) / 3);

    char *encoded_data = malloc(*output_length);
    if (encoded_data == NULL) return NULL;

    for (int i = 0, j = 0; i < input_length;) {

        uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
        uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
        uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;

        uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;

        encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
    }

    for (int i = 0; i < mod_table[input_length % 3]; i++)
        encoded_data[*output_length - 1 - i] = '=';

    return encoded_data;
}


unsigned char *base64_decode(const char *data,
                             size_t input_length,
                             size_t *output_length) {

    if (decoding_table == NULL) build_decoding_table();

    if (input_length % 4 != 0) return NULL;

    *output_length = input_length / 4 * 3;
    if (data[input_length - 1] == '=') (*output_length)--;
    if (data[input_length - 2] == '=') (*output_length)--;

    unsigned char *decoded_data = malloc(*output_length);
    if (decoded_data == NULL) return NULL;

    for (int i = 0, j = 0; i < input_length;) {

        uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];

        uint32_t triple = (sextet_a << 3 * 6)
        + (sextet_b << 2 * 6)
        + (sextet_c << 1 * 6)
        + (sextet_d << 0 * 6);

        if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
        if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
        if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
    }

    return decoded_data;
}


void build_decoding_table() {

    decoding_table = malloc(256);

    for (int i = 0; i < 64; i++)
        decoding_table[(unsigned char) encoding_table[i]] = i;
}


void base64_cleanup() {
    free(decoding_table);
}

Keep in mind that this doesn't do any error-checking while decoding - non base 64 encoded data will get processed.

2 of 16
126

I know this question is quite old, but I was getting confused by the amount of solutions provided - each one of them claiming to be faster and better. I put together a project on github to compare the base64 encoders and decoders: https://github.com/gaspardpetit/base64-benchmark/

At this point, I have not limited myself to C algorithms - if one implementation performs well in C++, it can easily be backported to C. Also tests were conducted using Visual Studio 2015. If somebody wants to update this answer with results from clang/gcc, be my guest.

FASTEST ENCODERS: The two fastest encoder implementations I found were Jouni Malinen's at http://web.mit.edu/freebsd/head/contrib/wpa/src/utils/base64.c and the Apache at https://github.com/apple-oss-distributions/apache1/blob/apache1-697/apache1/src/ap/ap_base64.c.

Here is the time (in microseconds) to encode 32K of data using the different algorithms I have tested up to now:

jounimalinen                25.1544
apache                      25.5309
NibbleAndAHalf              38.4165
internetsoftwareconsortium  48.2879
polfosol                    48.7955
wikibooks_org_c             51.9659
gnome                       74.8188
elegantdice                 118.899
libb64                      120.601
manuelmartinez              120.801
arduino                     126.262
daedalusalpha               126.473
CppCodec                    151.866
wikibooks_org_cpp           343.2
adp_gmbh                    381.523
LihO                        406.693
libcurl                     3246.39
user152949                  4828.21

(René Nyffenegger's solution, credited in another answer to this question, is listed here as adp_gmbh).

Here is the one from Jouni Malinen that I slightly modified to return a std::string:

/*
* Base64 encoding/decoding (RFC1341)
* Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/

// 2016-12-12 - Gaspard Petit : Slightly modified to return a std::string 
// instead of a buffer allocated with malloc.

#include <string>

static const unsigned char base64_table[65] =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/**
* base64_encode - Base64 encode
* @src: Data to be encoded
* @len: Length of the data to be encoded
* @out_len: Pointer to output length variable, or %NULL if not used
* Returns: Allocated buffer of out_len bytes of encoded data,
* or empty string on failure
*/
std::string base64_encode(const unsigned char *src, size_t len)
{
    unsigned char *out, *pos;
    const unsigned char *end, *in;

    size_t olen;

    olen = 4*((len + 2) / 3); /* 3-byte blocks to 4-byte */

    if (olen < len)
        return std::string(); /* integer overflow */

    std::string outStr;
    outStr.resize(olen);
    out = (unsigned char*)&outStr[0];

    end = src + len;
    in = src;
    pos = out;
    while (end - in >= 3) {
        *pos++ = base64_table[in[0] >> 2];
        *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
        *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
        *pos++ = base64_table[in[2] & 0x3f];
        in += 3;
    }

    if (end - in) {
        *pos++ = base64_table[in[0] >> 2];
        if (end - in == 1) {
            *pos++ = base64_table[(in[0] & 0x03) << 4];
            *pos++ = '=';
        }
        else {
            *pos++ = base64_table[((in[0] & 0x03) << 4) |
                (in[1] >> 4)];
            *pos++ = base64_table[(in[1] & 0x0f) << 2];
        }
        *pos++ = '=';
    }

    return outStr;
}

FASTEST DECODERS: Here are the decoding results and I must admit that I am a bit surprised:

polfosol                    45.2335
wikibooks_org_c             74.7347
apache                      77.1438
libb64                      100.332
gnome                       114.511
manuelmartinez              126.579
elegantdice                 138.514
daedalusalpha               151.561
jounimalinen                206.163
arduino                     335.95
wikibooks_org_cpp           350.437
CppCodec                    526.187
internetsoftwareconsortium  862.833
libcurl                     1280.27
LihO                        1852.4
adp_gmbh                    1934.43
user152949                  5332.87

Polfosol's snippet from Base64 decode snippet in C++ is the fastest by a factor of almost 2x.

Here is the code for the sake of completeness:

static const int B64index[256] = { 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 62, 63, 62, 62, 63, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61,  0,  0,  0,  0,  0,  0,  0,  0,  1,  2,  3,  4,  5,  6,
7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,  0,
0,  0,  0, 63,  0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };

std::string b64decode(const void* data, const size_t len)
{
    unsigned char* p = (unsigned char*)data;
    int pad = len > 0 && (len % 4 || p[len - 1] == '=');
    const size_t L = ((len + 3) / 4 - pad) * 4;
    std::string str(L / 4 * 3 + pad, '\0');

    for (size_t i = 0, j = 0; i < L; i += 4)
    {
        int n = B64index[p[i]] << 18 | B64index[p[i + 1]] << 12 | B64index[p[i + 2]] << 6 | B64index[p[i + 3]];
        str[j++] = n >> 16;
        str[j++] = n >> 8 & 0xFF;
        str[j++] = n & 0xFF;
    }
    if (pad)
    {
        int n = B64index[p[L]] << 18 | B64index[p[L + 1]] << 12;
        str[str.size() - 1] = n >> 16;

        if (len > L + 2 && p[L + 2] != '=')
        {
            n |= B64index[p[L + 2]] << 6;
            str.push_back(n >> 8 & 0xFF);
        }
    }
    return str;
}
🌐
MIT
web.mit.edu › freebsd › head › contrib › wpa › src › utils › base64.c
base64.c
*/ unsigned char * base64_encode(const unsigned char *src, size_t len, size_t *out_len) { unsigned char *out, *pos; const unsigned char *end, *in; size_t olen; int line_len; olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */ olen += olen / 72; /* line feeds */ olen++; /* nul termination */ if (olen < len) return NULL; /* integer overflow */ out = os_malloc(olen); if (out == NULL) return NULL; end = src + len; in = src; pos = out; line_len = 0; while (end - in >= 3) { *pos++ = base64_table[in[0] >> 2]; *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)]; *pos++ = base64_table[((in[1]
🌐
GitHub
github.com › rafagafe › base64
GitHub - rafagafe/base64: Base64 is a C library to convert from plain to base64 and vice versa suitable for embedded systems.
Base64 is a C library to convert from plain to base64 and vice versa suitable for embedded systems. - rafagafe/base64
Starred by 26 users
Forked by 8 users
Languages: C 95.6% | Makefile 4.4% | C 95.6% | Makefile 4.4%
Find elsewhere
🌐
GitHub
github.com › torvalds › linux › blob › master › lib › base64.c
linux/lib/base64.c at master · torvalds/linux
* base64.c - Base64 with support for multiple variants · * * Copyright (c) 2020 Hannes Reinecke, SUSE · * * Based on the base64url routines from fs/crypto/fname.c · * (which are using the URL-safe Base64 encoding), * modified to support multiple Base64 variants.
Author: torvalds
🌐
MYCPLUS
mycplus.com › home › programing source code examples › c programming source code examples
C Implementation of Base64 Encoding and Decoding - MYCPLUS
May 8, 2026 - libb64, OpenSSL Base64, Apple’s Implementations, arduino-base64 etc. #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <string.h> static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; static char *decoding_table = NULL; static int mod_table[] = {0, 2, 1}; void build_decoding_table
🌐
FreshPorts
freshports.org › converters › libb64
FreshPorts -- converters/libb64: Library for fast Base64 encoding and decoding
libb64 is a library of ANSI C routines for fast encoding/decoding of data into and from a Base64-encoded format. C++ wrappers are included, as well as the source code for standalone encoding and decoding executables. Base64 uses a subset of displayable ASCII characters, and is therefore a useful ...
🌐
Oryx-embedded
oryx-embedded.com › doc › base64_8c_source.html
base64.c Source Code - Base64 encoding scheme
void base64EncodeMultiline(const void *input, size_t inputLen, char_t *output, size_t *outputLen, size_t lineWidth)
🌐
Lua Ternary
nachtimwald.com › posts › base64 encode and decode in c
Base64 Encode and Decode in C | John's Blog
November 18, 2017 - This gives us a 4:3 ratio, meaning there is 33% overhead for base64. To get the number of base64 characters needed for a given binary data blob, take the length of input and round up to the nearest multiple of 3. Then, divide by 3 to get the number of 3 byte blocks.
🌐
GitHub
github.com › ZbrDeev › base64.c
GitHub - ZbrDeev/base64.c: Base64 library in C · GitHub
Quisque id erat id sem commodo " "volutpat a id lectus."; int main() { const char *encode = base64Encode(example, strlen(example)); const char *decode = base64Decode(encode, strlen(encode)); printf("encode: %s\n", encode); printf("decode: %s\n", decode); free((char *)encode); free((char *)decode); return 0; }
Author: ZbrDeev
🌐
Reddit
reddit.com › r/c_programming › wrote a simple base64 encoder/decoder and thought i'd share it.
r/C_Programming on Reddit: Wrote a simple base64 encoder/decoder and thought I'd share it.
March 31, 2017 -

So I needed to do some base64 encoding/decoding for a project I was working on and I was unsatisfied with the options I was finding. For the most part, people seemed to suggest using third party libraries that included base64 functions as part of a much larger set of tools (glib, for example), but I guess the purist in me wanted a simpler solution. So I ended up just writing my own encode/decode functions, and surprisingly it wasn't as complicated as I thought.

Still though, I am surprised at the lack of straight-forward solutions offered for C. Or did I just miss it somewhere? There are some projects on github but they are either poorly documented or seem unnecessarily complex. So I packaged up the functions I wrote, made the interface as dead-simple as possible, and put it on my github page for others who might also be searching for a solution:

base64-simple

Let me know if you think I missed a solution somewhere, or what you would have done for your base64 needs. Is using a library such a glib normal in this case? I'm also open to code critiques.

🌐
Base64
base64.dev › home › articles › base64 in c
Base64 Encoding & Decoding in C — base64.dev
July 3, 2026 - No. The C standard library (stdio.h, stdlib.h, string.h, etc.) does not include Base64 encoding.
🌐
Medium
medium.com › @js_9757 › revisiting-the-basics-implementing-base64-from-scratch-in-pure-c-c6985fb08420
Revisiting the Basics: Implementing Base64 from Scratch in pure C | by Jens@Fivesec | Medium
September 24, 2024 - Spoiler: Full working example can be found on Github · Base64 is an encoding method that converts data into characters that can be found in any Latin-based character set, making it displayable everywhere.
🌐
Malikania
projects.malikania.fr › libbase64
libbase64 - base64 encoding and decoding - malikania
The libbase64 is a minimal C library to decode and encore base64 in pure C89.
🌐
Doctrina
doctrina.org › Base64-With-OpenSSL-C-API.html
Doctrina - Base64 With OpenSSL C API
MakefileL7 - the C makefile. Compilation has been tested on a linux ubuntu distribution, and links with lcrypto for opensllL8 and lm for math. ... It is important to set the flag BIO_FLAGS_BASE64_NO_NL.