c++ base64 encode php decode,C++ Base64编码解码

/**

* @brief C++ base64 编解码

* @author wid

* @date 2013-20-25

*

* @note

*/

#include 

#include 

#include 

//base64 编解码函数声明

std::string b64encodestring(const std::string &strString);      //对 ASCII 字符串进行 base64 编码

std::string b64decodestring(const std::string &strString);      //对 base64 编码后的字符串进行解码

//base64 编解码函数实现

/**

* @brief 对 ASCII 字符串进行 base64 编码

*

* @param strString 待编码的字符串

*

* @return srs::string 返回编码后的字符串

*

* @note 对于字符串中含有非 ASCII 字符串型的字符, 代码将抛出 std::string 型异常, 请捕获

*/

std::string b64encodestring(const std::string &strString)

{

int nByteSrc = strString.length();

std::string pszSource = strString;

int i = 0;

for(i; i 

if( pszSource[i]  127 )

throw "can not encode Non-ASCII characters";

const char *enkey = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

std::string pszEncode(nByteSrc*4/3 + 4, '\0');

int nLoop = nByteSrc %3 == 0 ? nByteSrc : nByteSrc - 3;

int n = 0;

for(i=0; i 

{

pszEncode[n] = enkey[pszSource[i]>>2];

pszEncode[n+1] = enkey[((pszSource[i]&3)<<4) | ((pszSource[i+1] & 0xF0)>>4)];

pszEncode[n+2] = enkey[((pszSource[i+1] & 0x0f)<<2) | ((pszSource[i+2] & 0xc0 )>>6)];

pszEncode[n+3] = enkey[pszSource[i+2] & 0x3F];

n += 4;

}

switch(nByteSrc%3)

{

case 0:

pszEncode[n] = '\0';

break;

case 1:

pszEncode[n] = enkey[pszSource[i]>>2];

pszEncode[n+1] = enkey[((pszSource[i]&3)<<4) | ((0&0xf0)>>4)];

pszEncode[n+2] = '=';

pszEncode[n+3] = '=';

pszEncode[n+4] = '\0';

break;

case 2:

pszEncode[n] = enkey[pszSource[i]>>2];

pszEncode[n+1] = enkey[((pszSource[i]&3)<<4) | ((pszSource[i+1]&0xf0)>>4)];

pszEncode[n+2] = enkey[(( pszSource[i+1]&0xf)<<2 ) | ((0&0xc0)>>6)];

pszEncode[n+3] = '=';

pszEncode[n+4] = '\0';

break;

}

return pszEncode.c_str();

}

/**

* @brief 对 base64 编码后的字符串进行解码

*

* @param strString 待解码的字符串

*

* @return std::string 返回解码后的字符串

*

* @note 对于非base64编码的字符串或已损坏的base64字符串进行解码会抛出 std::string 型异常, 请捕获

*/

std::string b64decodestring(const std::string &strString)

{

int nByteSrc = strString.length();

std::string pszSource = strString;

const int dekey[] = {

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

62, // '+'

-1, -1, -1,

63, // '/'

52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'

-1, -1, -1, -1, -1, -1, -1,

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,

13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'

-1, -1, -1, -1, -1, -1,

26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,

39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'

};

if(nByteSrc%4 != 0)

throw "bad base64 string";

std::string pszDecode(nByteSrc*3/4+4, '\0');

int nLoop = pszSource[nByteSrc-1]  == '=' ? nByteSrc - 4 : nByteSrc;

int b[4];

int i = 0, n = 0;

for(i = 0; i 

{

b[0] = dekey[pszSource[i]];        b[1] = dekey[pszSource[i+1]];

b[2] = dekey[pszSource[i+2]];    b[3] = dekey[pszSource[i+3]];

if(b[0] == -1 || b[1] == -1 || b[2] == -1 || b[3] == -1)

throw "bad base64 string";

pszDecode[n] = (b[0] <> 4);

pszDecode[n+1] = ((b[1] & 0xf) <> 2);

pszDecode[n+2] =  ((b[2] & 0x3) <

n+=3;

}

if( pszSource[nByteSrc-1] == '=' && pszSource[nByteSrc-2] == '=' )

{

b[0] = dekey[pszSource[i]];        b[1] = dekey[pszSource[i+1]];

if(b[0] == -1 || b[1] == -1)

throw "bad base64 string";

pszDecode[n] = (b[0] <> 4);

pszDecode[n+1] = '\0';

}

if( pszSource[nByteSrc-1] == '=' && pszSource[nByteSrc-2] != '=' )

{

b[0] = dekey[pszSource[i]];        b[1] = dekey[pszSource[i+1]];

b[2] = dekey[pszSource[i+2]];

if(b[0] == -1 || b[1] == -1 || b[2] == -1)

throw "bad base64 string";

pszDecode[n] = (b[0] <> 4);

pszDecode[n+1] = ((b[1] & 0xf) <> 2);

pszDecode[n+2] = '\0';

}

if( pszSource[nByteSrc-1] != '=' && pszSource[nByteSrc-2] != '=' )

pszDecode[n] = '\0';

return pszDecode;

}

//测试

int main()

{

///编码测试

std::string str1 = "Hello, world!";

std::cout <

///解码测试

std::string str2 = "SGVsbG8sIHdvcmxkIQ==";

std::cout <

///编码耗时测试

std::string str3(10000000, 'A');    //生成 10000000 长的字符串

std::cout <

size_t t0 = clock();        //编码计时开始

b64encodestring(str3);

std::cout <

///解码耗时测试

std::string str4 = b64encodestring(str3);        //得到长度为 10000000 的字符串base64编码后的字符串

std::cout <

size_t t1 = clock();        //解码计时开始

b64decodestring(str3);

std::cout <

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 编码 Base64是一种用64个字符来表示任意二进制数据的编码方式。编码过程就是将二进制数据转换为可打印的ASCII字符集中的字符。 编码的过程如下: - 将传入的数据按照每3个字节一组进行划分。 - 对于每一组,将其转换为4个6位的数字,即24位。 - 将这4个数字转换为4个ASCII字符,即每个字符6位,不足6位的在后面补0。 - 如果原数据的长度不是3的倍数,则在末尾补0,然后用“=”字符表示补了几个0。 C语言中可以使用标准库中的函数实现Base64编码,具体实现如下: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> const char *base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; char *base64_encode(const unsigned char *data, size_t input_length, size_t *output_length) { *output_length = 4 * ((input_length + 2) / 3); char *encoded_data = malloc(*output_length + 1); if (encoded_data == NULL) return NULL; for (size_t 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 << 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 (size_t i = 0; i < (*output_length % 4); i++) encoded_data[*output_length - 1 - i] = '='; encoded_data[*output_length] = '\0'; return encoded_data; } 2. 解码 Base64解码过程就是将Base64编码的字符串转换为二进制数据。 解码的过程如下: - 将传入的字符串中的“=”字符去掉,将字符串长度调整为4的整数倍。 - 对于每4个字符,将其转换为3个6位的数字,即18位。 - 将这3个数字转换为3个字节的二进制数据。 - 如果编码的数据末尾有1或2个“=”字符,则表示解码后的数据末尾有1或2个字节为0。 C语言中可以使用标准库中的函数实现Base64解码,具体实现如下: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> const char *base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; int base64_decode_char(char c) { if (isupper(c)) return c - 'A'; if (islower(c)) return c - 'a' + 26; if (isdigit(c)) return c - '0' + 52; if (c == '+') return 62; if (c == '/') return 63; return -1; } unsigned char *base64_decode(const char *data, size_t input_length, size_t *output_length) { if (input_length % 4 != 0) return NULL; *output_length = input_length / 4 * 3; if (data[input_length - 1] == '=') (*output_length)--; if (data[input_length - 2] == '=') (*output_length)--; unsigned char *decoded_data = malloc(*output_length); if (decoded_data == NULL) return NULL; for (size_t i = 0, j = 0; i < input_length;) { uint32_t sextet_a = base64_decode_char(data[i++]); uint32_t sextet_b = base64_decode_char(data[i++]); uint32_t sextet_c = base64_decode_char(data[i++]); uint32_t sextet_d = base64_decode_char(data[i++]); uint32_t 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 decoded_data; } 以上就是Base64编码解码的C语言实现。使用时只需要调用相应的函数即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值