当前位置:   article > 正文

Base64的C语言实现_base64 c语言

base64 c语言

Base64相比于16进制hex码,更加陌生一些,其实原理相通,都是把二进制字节流转换为明文字符串。

16进制Hex码就是Base16,取16个字符0123456789abcdef,每2个字符表示1个字节;

Base64取64个字符ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

每4个字符表示3个字节。

本文给出Base64的C语言实现

Base64编码

  1. const char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  2. int base64_encode(const unsigned char *data, size_t input_length, char *encoded_data, int size_buf) {
  3. int output_length = 4 * ((input_length + 2) / 3);
  4. if(size_buf < output_length)
  5. return 0;
  6. if (encoded_data == NULL)
  7. return 0;
  8. int i;
  9. int j;
  10. for (i = 0, j = 0; i < input_length;) {
  11. unsigned int octet_a = i < input_length ? data[i++] : 0;
  12. unsigned int octet_b = i < input_length ? data[i++] : 0;
  13. unsigned int octet_c = i < input_length ? data[i++] : 0;
  14. unsigned int triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
  15. encoded_data[j++] = base64_table[(triple >> 3 * 6) & 0x3F];
  16. encoded_data[j++] = base64_table[(triple >> 2 * 6) & 0x3F];
  17. encoded_data[j++] = base64_table[(triple >> 1 * 6) & 0x3F];
  18. encoded_data[j++] = base64_table[(triple >> 0 * 6) & 0x3F];
  19. }
  20. for (i = 0; i < (3 - input_length % 3) % 3; i++) {
  21. encoded_data[output_length - 1 - i] = '=';
  22. }
  23. encoded_data[output_length] = '\0';
  24. return output_length;
  25. }

Base64解码,字码表base64_table与Base64编码相同

  1. int base64_decode_char(char c)
  2. {
  3. // strchr(base64_table, c) - base64_table;
  4. if (c >= 'A' && c <= 'Z') return c - 'A';
  5. if (c >= 'a' && c <= 'z') return c - 'a' + 26;
  6. if (c >= '0' && c <= '9') return c - '0' + 52;
  7. if (c == '+') return 62;
  8. if (c == '/') return 63;
  9. if (c == '=') return 0;
  10. return -1; // Invalid character
  11. }
  12. int base64_decode(const char *base64_string, unsigned char *decoded_data, int size_buf)
  13. {
  14. int input_length = strlen(base64_string);
  15. int output_length;
  16. if (input_length % 4 != 0)
  17. return 0; // Invalid input length
  18. output_length = input_length / 4 * 3;
  19. if (base64_string[input_length - 1] == '=') (output_length)--;
  20. if (base64_string[input_length - 2] == '=') (output_length)--;
  21. if(size_buf < output_length)
  22. return 0;
  23. int i;
  24. int j;
  25. for (i = 0, j = 0; i < input_length;) {
  26. unsigned int sextet_a = base64_decode_char(base64_string[i++]);
  27. unsigned int sextet_b = base64_decode_char(base64_string[i++]);
  28. unsigned int sextet_c = base64_decode_char(base64_string[i++]);
  29. unsigned int sextet_d = base64_decode_char(base64_string[i++]);
  30. if (sextet_a == -1 || sextet_b == -1 || sextet_c == -1 || sextet_d == -1) {
  31. return 0; // Invalid character
  32. }
  33. unsigned int triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6);
  34. if (j < output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
  35. if (j < output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
  36. if (j < output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
  37. }
  38. return output_length;
  39. }

测试

  1. int main()
  2. {
  3. // Base64 编码 =================================
  4. char base64String[1000] = {0};
  5. //unsigned char byteArray[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0xFF}; // 字节数组
  6. unsigned char byteArray[] = "hello world";
  7. int byteArrayLength = sizeof(byteArray) / sizeof(byteArray[0]);
  8. base64_encode(byteArray, byteArrayLength, base64String, sizeof(base64String));
  9. printf("Eecoded String: %s\n", base64String);
  10. // Base64 解码 =================================
  11. int i;
  12. unsigned char decodedByteArray[1000];
  13. //char *base64String = "SGVsbG8gV29ybGQh"; // Base64 编码的字符串
  14. //char *base64String = "AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTI=";
  15. int actualDecodedLength = base64_decode(base64String, decodedByteArray, sizeof(decodedByteArray));
  16. // 打印解码后的字节数组
  17. printf("Decoded Byte Array: ");
  18. for (i = 0; i < actualDecodedLength; i++)
  19. {
  20. printf("%02X ", decodedByteArray[i]);
  21. }
  22. printf("\n");
  23. for (i = 0; i < actualDecodedLength; i++)
  24. {
  25. printf("%c", decodedByteArray[i]);
  26. }
  27. printf("\n");
  28. return 0;
  29. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/863842
推荐阅读
相关标签
  

闽ICP备14008679号