c语言base64编解码

c语言base64编解码接口,移植了开源库b64.c的两个加解密接口

 

static const char b64_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', '+', '/'
};

char *b64_encode (const unsigned char *src, unsigned int len) {
  int i = 0;
  int j = 0;
  char *enc = NULL;
  unsigned int size = 0;
  unsigned char buf[4];
  unsigned char tmp[3];

  // alloc len/3 * 4 + 1
  enc = (char *) malloc(sizeof(char)*(len/3*4 + 1));
  if (NULL == enc) { return NULL; }

  // parse until end of source
  while (len--) {
    // read up to 3 bytes at a time into `tmp'
    tmp[i++] = *(src++);

    // if 3 bytes read then encode into `buf'
    if (3 == i) {
      buf[0] = (tmp[0] & 0xfc) >> 2;
      buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4);
      buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6);
      buf[3] = tmp[2] & 0x3f;

      // allocate 4 new byts for `enc` and
      // then translate each encoded buffer
      // part by index from the base 64 index table
      // into `enc' unsigned char array
      for (i = 0; i < 4; ++i) {
        enc[size++] = b64_table[buf[i]];
      }

      // reset index
      i = 0;
    }
  }

  // remainder
  if (i > 0) {
    // fill `tmp' with `\0' at most 3 times
    for (j = i; j < 3; ++j) {
      tmp[j] = '\0';
    }

    // perform same codec as above
    buf[0] = (tmp[0] & 0xfc) >> 2;
    buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4);
    buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6);
    buf[3] = tmp[2] & 0x3f;

    // perform same write to `enc` with new allocation
    for (j = 0; (j < i + 1); ++j) {
      enc[size++] = b64_table[buf[j]];
    }

    // while there is still a remainder
    // append `=' to `enc'
    while ((i++ < 3)) {
      enc[size++] = '=';
    }
  }

  // Make sure we have enough space to add '\0' character at end.
  enc[size] = '\0';
    
  return enc;
}



unsigned char *b64_decode(const char *src, unsigned int len, unsigned int *decsize) 
{
    int i = 0;
    int j = 0;
    int l = 0;
    unsigned int size = 0;
    unsigned char *dec = NULL;
    unsigned char buf[3];
    unsigned char tmp[4];
    unsigned int  str_len = 0; 

    //判断编码后的字符串后是否有=  
    if(strstr(src, "=="))  
        str_len = len/4*3 - 2;  
    else if(strstr(src, "="))  
        str_len = len/4*3 - 1;  
    else  
        str_len = len/4*3;  
  
    dec = malloc(sizeof(char)*str_len + 1);
    if(dec == NULL)
        return NULL;

  // parse until end of source
    while (len--) {
        // break if char is `=' or not base64 char
        if ('=' == src[j]) { break; }
        if (!(isalnum(src[j]) || '+' == src[j] || '/' == src[j])) { break; }

        // read up to 4 bytes at a time into `tmp'
        tmp[i++] = src[j++];

        // if 4 bytes read then decode into `buf'
        if (4 == i) {
          // translate values in `tmp' from table
            for (i = 0; i < 4; ++i) {
            // find translation char in `b64_table'
                for (l = 0; l < 64; ++l) {
                    if (tmp[i] == b64_table[l]) {
                        tmp[i] = l;
                        break;
                    }
                }
            }

            // decode
            buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4);
            buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2);
            buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3];

            // write decoded buffer to `dec'
            for (i = 0; i < 3; ++i) {
                dec[size++] = buf[i];
            }

            // reset
            i = 0;
        }
    }

    // remainder
    if (i > 0) {
        // fill `tmp' with `\0' at most 4 times
        for (j = i; j < 4; ++j) {
            tmp[j] = '\0';
        }

        // translate remainder
        for (j = 0; j < 4; ++j) {
            // find translation char in `b64_table'
            for (l = 0; l < 64; ++l) {
              if (tmp[j] == b64_table[l]) {
                tmp[j] = l;
                break;
              }
            }
        }

        // decode remainder
        buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4);
        buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2);
        buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3];

        // write remainer decoded buffer to `dec'
        for (j = 0; (j < i - 1); ++j) {
          dec[size++] = buf[j];
        }

    }

  // Make sure we have enough space to add '\0' character at end.
    dec[size] = '\0';
  
  // Return back the size of decoded string if demanded.
    if (decsize != NULL) {
        *decsize = size;
    }

  return dec;
}


 

int main (void) 
{

  // encode
  char *encode_data = "qwerty12345678345+=/&^%$#@!~*()[][]{}?.,";
  char *pe = NULL;
  char *pd = NULL;
  pe = b64_encode ((const unsigned char*)encode_data, strlen(encode_data));
  printf("encode: %s\n", pe);

  // decode
  pd = (char*)b64_decode (pe, strlen(pe));
  printf("decode: %s\n", pd);

  
  ly_free(pe);
  ly_free(pd);
  return 0;
}


 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值