Information Security 21 min read

Base64 Algorithm Basics, Encoding/Decoding Principles and Java & C++ Implementations

This article introduces the fundamentals of the Base64 encoding algorithm, explains its role in reverse‑engineering and security scenarios, details the encoding and decoding processes, and provides complete Java and native C++ implementations with code examples for Android development.

Top Architect
Top Architect
Top Architect
Base64 Algorithm Basics, Encoding/Decoding Principles and Java & C++ Implementations

In this chapter the author introduces common encryption and encoding algorithms, focusing on Base64, and explains their relevance in reverse‑engineering scenarios such as web crawlers and security protection.

Algorithm Basics

The author classifies algorithms into common encodings (Base16, Base32, Base64), hash functions (MD5, SHA‑1, SHA‑2) and symmetric ciphers (DES, 3DES, AES).

Base64 Overview

Base64 uses a 64‑character table to represent binary data, converting every 3 bytes (24 bits) into 4 printable characters. It is widely used to transmit binary data over text‑only protocols.

Encoding Principle

The 24‑bit block is split into four 6‑bit groups, each used as an index into the Base64 table. Padding with “=” is added when the input length is not a multiple of three.

Decoding Principle

Decoding reverses the process using a reverse lookup table; padding characters are ignored.

Java Implementation

String str = "qwertyuiopasdfghjklzxcvbnm0123456789~!@#$%^&*()_+`¥……——+|《》?,./城市 姓名";
byte[] byteStr = str.getBytes("utf-8");
String encode_DEFAULT = Base64.encodeToString(byteStr, Base64.DEFAULT);
String encode_NO_PADDING = Base64.encodeToString(byteStr, Base64.NO_PADDING);
String encode_NO_WRAP = Base64.encodeToString(byteStr, Base64.NO_WRAP);
String encodeURL_SAFE = Base64.encodeToString(byteStr, Base64.URL_SAFE);

C++ Native Implementation

void base64_encode(const char* data, char* out) {
    int data_len = strlen(data);
    if (data_len == 0) { out[0] = '\0'; return; }
    int index = 0; char c = '\0'; char last_c = '\0';
    for (int i = 0; i < data_len; i++) {
        c = data[i];
        switch (i % 3) {
            case 0: out[index++] = base64en[(c >> 2) & 0x3f]; break;
            case 1: out[index++] = base64en[(last_c & 0x3) << 4 | ((c >> 4) & 0xf)]; break;
            case 2: out[index++] = base64en[((last_c & 0xf) << 2 | ((c >> 6) & 0x3))]; out[index++] = base64en[c & 0x3f]; break;
        }
        last_c = c;
    }
    if (data_len % 3 == 1) { out[index++] = base64en[(c & 0x3) << 4]; out[index++] = '='; out[index++] = '='; }
    if (data_len % 3 == 2) { out[index++] = base64en[(c & 0xf) << 2]; out[index++] = '='; }
}

void base64_decode(char* data, char* output) {
    int data_len = strlen(data);
    unsigned char c = '\0'; int t = 0, y = 0, i = 0; int g = 3;
    for (int x = 0; x < data_len; x++) {
        c = base64_suffix_map[data[x]];
        if (c == 255) { output[0] = '\0'; return; }
        if (c == 253) continue; // newline or carriage return
        if (c == 254) { c = 0; g--; } // '='
        t = (t << 6) | c;
        if (++y == 4) {
            output[i++] = (t >> 16) & 0xff;
            if (g > 1) output[i++] = (t >> 8) & 0xff;
            if (g > 2) output[i++] = t & 0xff;
            y = t = 0;
        }
    }
}

The article provides complete source code, explains each step of the encoding/decoding loops, and shows how to handle remaining one or two bytes.

Summary

By mastering the Base64 algorithm and its implementations in Java and C++, readers can decode or protect data in Android applications and reverse‑engineering tasks.

AlgorithmAndroidCSecurityEncryptionBase64
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.