Base64 Encoder/Decoder

Encode text to Base64 or decode Base64 strings. Supports UTF-8 encoding.

Why Base64? Base64 was developed to solve a fundamental problem: early email systems (SMTP) only supported 7-bit ASCII characters and couldn't transmit binary data directly. By converting binary data into ASCII text, Base64 enabled email attachments and became essential for text-only protocols and systems.

Use cases: Encoding data for URLs, email attachments, storing binary data in JSON/XML/HTML, embedding images in CSS/HTML (data URIs), encoding credentials, and ensuring safe transmission through text-only systems.

Note: Base64 increases data size by ~33% and is encoding, not encryption. This tool handles UTF-8 text encoding/decoding automatically.

FUNCTION encodeBase64(input):
    bytes = convertToBytes(input)
    base64Chars = "A-Z, a-z, 0-9, +, /"
    result = ""
    i = 0
    
    WHILE i < length(bytes):
        // Get 3 bytes (24 bits)
        byte1 = bytes[i]
        byte2 = bytes[i + 1] IF i + 1 < length(bytes) ELSE 0
        byte3 = bytes[i + 2] IF i + 2 < length(bytes) ELSE 0
        
        // Combine into 24-bit number
        combined = (byte1 << 16) | (byte2 << 8) | byte3
        
        // Extract 4 groups of 6 bits
        char1 = (combined >> 18) & 0x3F
        char2 = (combined >> 12) & 0x3F
        char3 = (combined >> 6) & 0x3F
        char4 = combined & 0x3F
        
        // Map to Base64 characters
        result += base64Chars[char1]
        result += base64Chars[char2]
        
        // Handle padding
        IF i + 1 >= length(bytes):
            result += "=="
        ELSE IF i + 2 >= length(bytes):
            result += base64Chars[char3] + "="
        ELSE:
            result += base64Chars[char3]
            result += base64Chars[char4]
        
        i += 3
    
    RETURN result