乐闻世界logo
搜索文章和话题

How do I base64 encode ( decode ) in C?

1个答案

1

Implementing Base64 encoding and decoding in C involves specific transformations of data. Base64 encoding is primarily used in scenarios where binary data needs to be converted into printable characters, such as sending images in email protocols. I will now provide a detailed explanation of how to implement this functionality in C.

Base64 Encoding Principles

Base64 encoding uses a set of 64 characters (A-Z, a-z, 0-9, +, /), where each 6-bit unit is converted into a printable character. During encoding, groups of three bytes are processed, and these 24 bits are divided into four 6-bit units. If the last group has fewer than three bytes, '=' is used for padding.

Implementation Steps

  1. Prepare the Encoding Table: Create a character array containing all Base64 characters.
  2. Group Data Processing: Process the raw data in groups of three bytes.
  3. Convert to 6-bit Units: Convert three bytes (24 bits) into four 6-bit numbers.
  4. Lookup for Encoding Result: Use the values from the previous step as indices to find the corresponding characters in the encoding table.
  5. Add Padding Characters: If the data byte count is not a multiple of three, add one or two '=' characters for padding.

Example Code

Here is a simple example of Base64 encoding in C:

c
#include <stdio.h> #include <string.h> static const char encoding_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static char decoding_table[256]; static int mod_table[] = {0, 2, 1}; void base64_encode(const unsigned char *data, size_t input_length, char *encoded_data) { size_t output_length = 4 * ((input_length + 2) / 3); for (int i = 0, j = 0; i < input_length;) { uint32_t octet_a = i < input_length ? data[i++] : 0; uint32_t octet_b = i < input_length ? data[i++] : 0; uint32_t octet_c = i < input_length ? data[i++] : 0; uint32_t triple = (octet_a << 16) + (octet_b << 8) + octet_c; encoded_data[j++] = encoding_table[(triple >> 18) & 0x3F]; encoded_data[j++] = encoding_table[(triple >> 12) & 0x3F]; encoded_data[j++] = encoding_table[(triple >> 6) & 0x3F]; encoded_data[j++] = encoding_table[triple & 0x3F]; } for (int i = 0; i < mod_table[input_length % 3]; i++) encoded_data[output_length - 1 - i] = '='; encoded_data[output_length] = '\0'; } int main() { const unsigned char data[] = "Hello, World!"; size_t input_length = sizeof(data) - 1; char encoded_data[20]; base64_encode(data, input_length, encoded_data); printf("Base64 Encoded: %s\n", encoded_data); return 0; }

This code demonstrates how to encode the string 'Hello, World!' using Base64. The encoding function base64_encode takes the raw data and its length as inputs and outputs the encoded string. This implementation simply demonstrates the encoding process but does not include the decoding process. To implement decoding, you can follow a similar approach by using the table to convert each character back to its original 6-bit units and then combine them into the original bytes.

2024年8月23日 18:08 回复

你的答案