赞
踩
Base64相比于16进制hex码,更加陌生一些,其实原理相通,都是把二进制字节流转换为明文字符串。
16进制Hex码就是Base16,取16个字符0123456789abcdef,每2个字符表示1个字节;
Base64取64个字符ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
每4个字符表示3个字节。
本文给出Base64的C语言实现
- const char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
- int base64_encode(const unsigned char *data, size_t input_length, char *encoded_data, int size_buf) {
- int output_length = 4 * ((input_length + 2) / 3);
- if(size_buf < output_length)
- return 0;
-
- if (encoded_data == NULL)
- return 0;
- int i;
- int j;
-
- for (i = 0, j = 0; i < input_length;) {
- unsigned int octet_a = i < input_length ? data[i++] : 0;
- unsigned int octet_b = i < input_length ? data[i++] : 0;
- unsigned int octet_c = i < input_length ? data[i++] : 0;
-
- unsigned int triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
-
- encoded_data[j++] = base64_table[(triple >> 3 * 6) & 0x3F];
- encoded_data[j++] = base64_table[(triple >> 2 * 6) & 0x3F];
- encoded_data[j++] = base64_table[(triple >> 1 * 6) & 0x3F];
- encoded_data[j++] = base64_table[(triple >> 0 * 6) & 0x3F];
- }
-
- for (i = 0; i < (3 - input_length % 3) % 3; i++) {
- encoded_data[output_length - 1 - i] = '=';
- }
-
- encoded_data[output_length] = '\0';
- return output_length;
- }
- int base64_decode_char(char c)
- {
- // strchr(base64_table, c) - base64_table;
-
- if (c >= 'A' && c <= 'Z') return c - 'A';
- if (c >= 'a' && c <= 'z') return c - 'a' + 26;
- if (c >= '0' && c <= '9') return c - '0' + 52;
- if (c == '+') return 62;
- if (c == '/') return 63;
- if (c == '=') return 0;
-
- return -1; // Invalid character
- }
-
- int base64_decode(const char *base64_string, unsigned char *decoded_data, int size_buf)
- {
- int input_length = strlen(base64_string);
- int output_length;
-
- if (input_length % 4 != 0)
- return 0; // Invalid input length
-
- output_length = input_length / 4 * 3;
- if (base64_string[input_length - 1] == '=') (output_length)--;
- if (base64_string[input_length - 2] == '=') (output_length)--;
- if(size_buf < output_length)
- return 0;
- int i;
- int j;
-
- for (i = 0, j = 0; i < input_length;) {
- unsigned int sextet_a = base64_decode_char(base64_string[i++]);
- unsigned int sextet_b = base64_decode_char(base64_string[i++]);
- unsigned int sextet_c = base64_decode_char(base64_string[i++]);
- unsigned int sextet_d = base64_decode_char(base64_string[i++]);
-
- if (sextet_a == -1 || sextet_b == -1 || sextet_c == -1 || sextet_d == -1) {
- return 0; // Invalid character
- }
-
- unsigned int triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6);
-
- if (j < output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
- if (j < output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
- if (j < output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
- }
-
- return output_length;
- }
- int main()
- {
-
- // Base64 编码 =================================
- char base64String[1000] = {0};
-
- //unsigned char byteArray[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0xFF}; // 字节数组
- unsigned char byteArray[] = "hello world";
-
- int byteArrayLength = sizeof(byteArray) / sizeof(byteArray[0]);
- base64_encode(byteArray, byteArrayLength, base64String, sizeof(base64String));
- printf("Eecoded String: %s\n", base64String);
-
-
- // Base64 解码 =================================
- int i;
- unsigned char decodedByteArray[1000];
-
- //char *base64String = "SGVsbG8gV29ybGQh"; // Base64 编码的字符串
- //char *base64String = "AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTI=";
- int actualDecodedLength = base64_decode(base64String, decodedByteArray, sizeof(decodedByteArray));
-
- // 打印解码后的字节数组
- printf("Decoded Byte Array: ");
- for (i = 0; i < actualDecodedLength; i++)
- {
- printf("%02X ", decodedByteArray[i]);
- }
- printf("\n");
- for (i = 0; i < actualDecodedLength; i++)
- {
- printf("%c", decodedByteArray[i]);
- }
- printf("\n");
-
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。