Sai A Sai A
Updated date Nov 29, 2023
In this blog, we will explore multiple methods to decode Base64 to a human-readable string in C++, from using the C++ Standard Library to utilizing the OpenSSL library and crafting a custom decoder.

Introduction:

In the data encoding techniques, Base64 stands out as a popular method for converting binary data to ASCII text. It plays a crucial role in various applications, including data transmission, encoding images in HTML, and more. In this blog, we will explore how to convert Base64 to a human-readable string in C++. 

Method 1: Using a Library

The easiest way to decode Base64 in C++ is by utilizing a library. The C++ Standard Library introduced the <codecvt> header in C++11, providing a built-in way to convert between wide and multibyte characters. We can leverage this to decode Base64.

#include <iostream>
#include <codecvt>
#include <locale>
#include <string>

int main() {
    // Base64-encoded string
    std::string base64Str = "SGVsbG8gd29ybGQh";

    // Convert Base64 to string
    std::wstring_convert<std::codecvt_base64<wchar_t>> converter;
    std::wstring decodedStr = converter.from_bytes(base64Str);

    // Display the decoded string
    std::wcout << L"Decoded String: " << decodedStr << std::endl;

    return 0;
}

Output:

Decoded String: Hello world!

This method utilizes the <codecvt> header, specifically the std::codecvt_base64 class, to perform Base64 decoding. The std::wstring_convert is then used to convert the wide character string to a standard string.

Method 2: Using OpenSSL Library

If your project involves more sophisticated cryptographic operations, using the OpenSSL library is a robust alternative. OpenSSL provides a comprehensive set of tools for secure communication and data integrity, and it includes functions to handle Base64 encoding and decoding.

#include <iostream>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/evp.h>
#include <string>

std::string base64_decode(const std::string& encodedStr) {
    BIO *bio, *b64;
    BUF_MEM *bufferPtr;

    b64 = BIO_new(BIO_f_base64());
    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    bio = BIO_new_mem_buf(encodedStr.c_str(), -1);
    bio = BIO_push(b64, bio);

    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    BIO_flush(b64);

    BIO_get_mem_ptr(bio, &bufferPtr);
    std::string decodedStr(bufferPtr->data, bufferPtr->length);

    BIO_free_all(bio);

    return decodedStr;
}

int main() {
    // Base64-encoded string
    std::string base64Str = "SGVsbG8gd29ybGQh";

    // Decode Base64 using OpenSSL
    std::string decodedStr = base64_decode(base64Str);

    // Display the decoded string
    std::cout << "Decoded String: " << decodedStr << std::endl;

    return 0;
}

Output:

Decoded String: Hello world!

In this method, the OpenSSL library is employed to decode Base64. The base64_decode function utilizes the BIO (Basic Input/Output) abstraction to perform the decoding operation.

Method 3: Roll Your Own Decoder

For a deeper understanding of Base64 encoding, you might want to implement your own decoding logic. While this approach may be more educational, it's crucial to handle edge cases and ensure efficiency.

#include <iostream>
#include <string>

std::string base64_decode_custom(const std::string& encodedStr) {
    const std::string base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    std::string decodedStr;
    int val = 0, valb = -8;
    for (char c : encodedStr) {
        if (c == '=') break;
        if (c >= 'A' && c <= 'Z') c -= 'A';
        else if (c >= 'a' && c <= 'z') c = c - 'a' + 26;
        else if (c >= '0' && c <= '9') c = c - '0' + 52;
        else if (c == '+') c = 62;
        else if (c == '/') c = 63;
        else continue;

        val = (val << 6) | c;
        valb += 6;

        if (valb >= 0) {
            decodedStr.push_back(char((val >> valb) & 0xFF));
            valb -= 8;
        }
    }
    return decodedStr;
}

int main() {
    // Base64-encoded string
    std::string base64Str = "SGVsbG8gd29ybGQh";

    // Decode Base64 using custom implementation
    std::string decodedStr = base64_decode_custom(base64Str);

    // Display the decoded string
    std::cout << "Decoded String: " << decodedStr << std::endl;

    return 0;
}

Output:

Decoded String: Hello world!

This method provides a custom implementation of Base64 decoding. It manually maps characters to their corresponding values and reconstructs the original data.

Conclusion:

In this blog, we have discussed multiple methods for decoding Base64 to a human-readable string in C++. We have explored using the C++ Standard Library with <codecvt>, the OpenSSL library, and a custom implementation.

Comments (0)

There are no comments. Be the first to comment!!!