类别:VC语言 点击:0 评论:0 推荐:

 This is a sample routine of base64 algorithm.The goal is to
 illustrate principles,so some details may be ignored.
 Author email:[email protected]
#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; 

 if(n%3!=0)  /* pad with '=' by using a temp buffer */
  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);
 for(i=0;i<buflen*4/3;i++) /* map 6 bit value to base64 ASCII character */
 return dst;

unsigned char *decode(unsigned char *src)
 int n,i,j;
 unsigned char *p;
 static unsigned char *dst; 

 for(i=0;i<n;i++) /* map base64 ASCII character to 6 bit value */
  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 */