C 语言实现 php base64_encode
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C 语言实现 php base64_encode相关的知识,希望对你有一定的参考价值。
这是在网上找到的一段代码,因为需求不同,稍微做了下修改,有需要的朋友可以直接复制使用。
unsigned char *base64_encode(const unsigned char *str, size_t length) { if (NULL == str || 0 == length) return NULL; static const char base64_table[] = { ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘, ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘+‘, ‘/‘, ‘\0‘ }; static const char base64_pad = ‘=‘; const unsigned char *current = str; //int length = strlen(str); unsigned char *p; unsigned char *result; if ((length + 2) < 0 || ((length + 2) / 3) >= (1 << (sizeof(int) * 8 - 2))) { return NULL; } result = (unsigned char *)malloc(((length + 2) / 3) * 4 * sizeof(char) + 1); p = result; while (length > 2) { *p++ = base64_table[current[0] >> 2]; *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; *p++ = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)]; *p++ = base64_table[current[2] & 0x3f]; current += 3; length -= 3; } if (length != 0) { *p++ = base64_table[current[0] >> 2]; if (length > 1) { *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; *p++ = base64_table[(current[1] & 0x0f) << 2]; *p++ = base64_pad; } else { *p++ = base64_table[(current[0] & 0x03) << 4]; *p++ = base64_pad; *p++ = base64_pad; } } *p = ‘\0‘; return result; }
以上是关于C 语言实现 php base64_encode的主要内容,如果未能解决你的问题,请参考以下文章
java 怎么实现PHP的base64加密,两种语言的base64加密后的数据不一致
base64_encode(file_get_contents(zipPath))c#等效
PHP base64_encode 在URL地址参数编码上使用
PHP:base64_encode 是不是可以防止 mysql 注入?