Base64编码解码的实现(C语言)

base64编码解码的实现(C语言)




 /****************************************************************

This is a sample routine of base64 algorithm.The goal is to


illustrate principles,so some details may be ignored.


Author email:zhangwu2003@163.com


*****************************************************************/


#include <stdio.h>


#include <string.h>


#include <malloc.h>


char*

ch64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

unsigned char *encode(unsigned char *src,int srclen)


{


int n,buflen,i,j;


int pading=0;


unsigned char *buf;


static unsigned char *dst;

buf=src;


buflen=n=srclen;


if(n%3!=0)   /* pad with ´=´ by using a temp buffer */


{


   pading=1;


   buflen=n+3-n%3;


   buf=malloc(buflen+1);


   memset(buf,0,buflen+1);


   memcpy(buf,src,n);


   for(i=0;i<3-n%3;i++)


    buf[n+i]=´=´;


}


dst=malloc(buflen*4/3+1);


memset(dst,0,buflen*4/3+1);


for(i=0,j=0;i<buflen;i+=3,j+=4)


{


   dst[j]=(buf[i]&0xFC)>>2;


   dst[j+1]=((buf[i]&0x03)<<4) + ((buf[i+1]&0xF0)>>4);


   dst[j+2]=((buf[i+1]&0x0F)<<2) + ((buf[i+2]&0xC0)>>6);


   dst[j+3]=buf[i+2]&0x3F;


}


for(i=0;i<buflen*4/3;i++) /* map 6 bit value to base64 ASCII character */


   dst[i]=ch64[dst[i]];


if(pading)


   free(buf);


return dst;


}

unsigned char *decode(unsigned char *src)


{


int n,i,j;


unsigned char *p;


static unsigned char *dst;

n=strlen(src);


for(i=0;i<n;i++) /* map base64 ASCII character to 6 bit value */


{


   p=strchr(ch64,src[i]);


   if(!p)


    break;


   src[i]=p-ch64;


}


dst=malloc(n*3/4+1);


memset(dst,0,n*3/4+1);


for(i=0,j=0;i<n;i+=4,j+=3)


{


   dst[j]=(src[i]<<2) + ((src[i+1]&0x30)>>4);


   dst[j+1]=((src[i+1]&0x0F)<<4) + ((src[i+2]&0x3C)>>2);


   dst[j+2]=((src[i+2]&0x03)<<6) + src[i+3];


}


return dst;


}

void main()


{


// char *src="zhangwu张武";


char src[]={´1´,´2´,´3´,0,´a´,´b´,´*´,0,´A´,´B´,´$´};


unsigned char *dst1;


unsigned char *dst2;


unsigned int i;

dst1=encode(src,11); /* the second parameter must accord with the first one */


printf("%s/n",dst1);


dst2=decode(dst1);


for(i=0;i<_msize(dst2);i++)


   printf("%c",dst2[i]);

free(dst1);


free(dst2);


}

 

=================

 

base64编码的实现

int Base64Enc(char *buf, char*text,int size) 

static char *base64_encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int buflen = 0; 

while(size>0)
{
*buf++ = base64_encoding[ (text[0] >> 2 ) & 0x3f];
if(size>2)
{
*buf++ = base64_encoding[((text[0] & 3) << 4) | (text[1] >> 4)];
*buf++ = base64_encoding[((text[1] & 0xF) << 2) | (text[2] >> 6)];
*buf++ = base64_encoding[text[2] & 0x3F];
}
else
{
switch(size)
{
case 1:
*buf++ = base64_encoding[(text[0] & 3) << 4 ];
*buf++ = '=';
*buf++ = '=';
break;
case 2: 
*buf++ = base64_encoding[((text[0] & 3) << 4) | (text[1] >> 4)]; 
*buf++ = base64_encoding[((text[1] & 0x0F) << 2) | (text[2] >> 6)]; 
*buf++ = '='; 
break; 



text +=3; 
size -=3; 
buflen +=4; 


*buf = 0; 
return buflen; 
}

base64解码的实现

char GetBase64Value(char ch)
{
if ((ch >= 'A') && (ch <= 'Z')) 
return ch - 'A'; 
if ((ch >= 'a') && (ch <= 'z')) 
return ch - 'a' + 26; 
if ((ch >= '0') && (ch <= '9')) 
return ch - '0' + 52; 
switch (ch) 

case '+': 
return 62; 
case '/': 
return 63; 
case '=': /* base64 padding */ 
return 0; 
default: 
return 0; 

}

//进行base64解码输入应该是4的倍数(根据mime标准)
//如果不是4倍数返回错误
//注意 如果是最后一个字符 那么长度不准备 可能会多1 
//返回buf长度
int Base64Dec(char *buf,char*text,int size) 
{
if(size%4)
return -1;
unsigned char chunk[4];
int parsenum=0;

while(size>0)
{
chunk[0] = GetBase64Value(text[0]); 
chunk[1] = GetBase64Value(text[1]); 
chunk[2] = GetBase64Value(text[2]); 
chunk[3] = GetBase64Value(text[3]); 

*buf++ = (chunk[0] << 2) | (chunk[1] >> 4); 
*buf++ = (chunk[1] << 4) | (chunk[2] >> 2); 
*buf++ = (chunk[2] << 6) | (chunk[3]);

text+=4;
size-=4;
parsenum+=3;
}

return parsenum;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值