How to Implement Base64 Encoding and Decoding in C

Learn how to implement Base64 encoding and decoding functions in C using tables and code examples.
On this page

How to Implement Base64 Encoding and Decoding in C

Excerpt

Master Base64 encoding and decoding in C. This guide covers the encoding algorithm, implementing encode/decode functions in C, command line programs, and use cases.


Introduction to Base64

Base64 is an encoding scheme that allows binary data to be transmitted over textual mediums. It works by converting binary data into a text format using 64 printable ASCII characters.

What is Base64 Encoding?

Base64 encoding takes binary data like file contents or network packets and converts it into an ASCII string format. This Base64 text can be safely transmitted over systems that only support ASCII characters.

It is commonly used to encode data for transport over HTTP, storing complex data in databases, and encoding file attachments in emails.

Why Use Base64 Encoding?

There are several advantages to encoding binary data as Base64:

  • Allows transmission of binary data over text-only protocols
  • Universally compatible across languages and platforms
  • Base64 strings are easy to embed in code and filenames
  • Used for obfuscating encrypted data by encoding it

Overview of Encoding and Decoding

  • Encoding: Binary data is split into 3 byte chunks, converted to 4 Base64 characters
  • Decoding: Base64 text is converted back 4 chars at a time to the original binary data

Decoding reverses the encoding process to reconstruct the original data.

Understanding the Base64 Encoding Algorithm

Base64 encoding works by breaking data into chunks and mapping each chunk to a character.

How Data is Encoded

The encoding process consists of:

  1. Break data into chunks of 3 bytes (24 bits)
  2. Divide the 24 bits into 4 groups of 6 bits
  3. Map each 6-bit group to a character in the Base64 alphabet

This mapping is what converts the raw binary data into encoded ASCII text.

Base64 Encoding Table

The Base64 character set contains 64 characters:

1A-Z, a-z, 0-9, +, /

Each character encodes 6 bits of data. This is how each 3 byte chunk of data gets converted into 4 Base64 characters.

Implementing Base64 Encoding in C

Here is how to implement a Base64 encoding function in C:

Include Required Header Files

1#include <stdio.h>
2#include <stdlib.h>

Define Encoding Table

1char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ...";

This holds the Base64 character set.

Encode Function

1char* b64encode(const unsigned char* data) {
2
3  // Encoding steps...
4
5  return encoded_string;
6
7}

Key steps:

  • Accept raw binary input data
  • Divide data into 3-byte blocks
  • Convert each 3 bytes to a 6-bit index
  • Lookup encoded char from encoding table
  • Return final encoded string

Example Encoding

1char* input = "IToolkit";
2char* encoded = b64encode(input); // Outputs SVRvb2xraXQ=

The free Base64 Encode verification tool is as follows:

Implementing Base64 Decoding in C

We also need a function to decode Base64 strings back to binary data:

Define Decoding Table

1int b64chars_rev[128]; // Reverse mapping

This maps characters back to their 6-bit index.

Decode Function

1unsigned char* b64decode(const char* data) {
2
3  // Decoding steps...
4
5  return decoded_data;
6
7}

Key steps:

  • Accept encoded Base64 input
  • Break into 4 character blocks
  • Map each char to 6-bit index
  • Reconstruct 8-bit bytes from 6-bit groups
  • Return decoded binary data

Example Decoding

1char* encoded = "SVRvb2xraXQ=";
2unsigned char* decoded = b64decode(encoded); // Outputs bytes "IToolkit"

The free Base64 Decode verification tool is as follows:

Complete Encode/Decode Program in C

For a reusable implementation, we can combine encoding and decoding into a single program:

Add Command Line Arguments

 1int main(int argc, char* argv[]) {
 2
 3  if(argc < 2) {
 4    printf("Usage: program_name <encode/decode> <values>");
 5    return 1;
 6  }
 7
 8  char* mode = argv[1];
 9  char* data = argv[2];
10
11}

This allows specifying encode or decode plus the input from command line.

Support Files or Standard Input

 1if (mode == "encode") {
 2
 3  if (data == "-") {
 4    // Read binary data from standard input
 5
 6  } else {
 7    // Open file and read data
 8
 9  }
10
11}

Check if data is - to read from standard input stream.

Encode/Decode Based on Input

1if (mode == "encode") {
2  char* encoded = b64encode(data);
3
4} else {
5  unsigned char* decoded = b64decode(data);
6}

Call the appropriate function.

Conclusion

  • Base64 encoding allows binary data transmission over text
  • Implemented in C using encoding/decoding tables
  • Encoder splits data into 3-byte blocks and maps to 4 characters
  • Decoder converts characters back to 6-bit groups
  • Command line program supports files or standard input streams

Base64 encoding is used universally across many platforms and languages. By implementing Base64 functions in C, you gain lower-level control and understanding of the encoding process.