Base64 in C++

How to encode and decode Base64 in C++ — with copy-paste examples and a live converter to check your output.

The C++ standard library has no Base64 support, so most projects either link OpenSSL/Boost or drop in a small lookup-table implementation.

Encode to Base64 in C++

C++
#include <openssl/evp.h>

int len = EVP_EncodeBlock(reinterpret_cast<unsigned char*>(out),
                           reinterpret_cast<const unsigned char*>(in), in_len);

Decode Base64 in C++

C++
#include <openssl/evp.h>

int len = EVP_DecodeBlock(reinterpret_cast<unsigned char*>(out),
                           reinterpret_cast<const unsigned char*>(in), in_len);

Notes & gotchas

EVP_EncodeBlock/EVP_DecodeBlock (from OpenSSL) are a common zero-dependency-beyond-OpenSSL choice; Boost.Beast and Boost.Archive also ship Base64 codecs. Note EVP_DecodeBlock does not strip trailing padding bytes from the output length automatically — you must account for "=" padding yourself.

Try it live

0 characters

Base64 in C++ — FAQ

How do I Base64 encode a string in C++?+

Use the code shown above. EVP_EncodeBlock/EVP_DecodeBlock (from OpenSSL) are a common zero-dependency-beyond-OpenSSL choice; Boost.Beast and Boost.Archive also ship Base64 codecs. Note EVP_DecodeBlock does not strip trailing padding bytes from the output length automatically — you must account for "=" padding yourself.

How do I decode Base64 back to text in C++?+

Use the decode snippet above. Base64 decoding is lossless, so you get the exact original bytes back; decode them with UTF-8 to recover text.

Is Base64 encoding the same across programming languages?+

Yes. Base64 is a standard (RFC 4648), so a string encoded in one language decodes correctly in any other. Only the API differs, not the output.

Does Base64 secure my data?+

No. Base64 is an encoding, not encryption — anyone can decode it. Never use it to protect secrets.