Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1343718
  • 博文数量: 436
  • 博客积分: 7854
  • 博客等级: 少将
  • 技术积分: 3225
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-18 16:30
文章分类

全部博文(436)

文章存档

2013年(2)

2012年(56)

2011年(70)

2010年(308)

分类:

2011-03-22 14:25:15

Base64是一种网络上十分常见的编码算法,主要是对ASCII码进行编码,网络上经常用它来对8Bit字节代码进行编码,编码后的字符是数组table[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef

ghijklmnopqrstuvwxyz01234567890+/", 即:26的大小,写字母,10个数字,一个"+",一个"/",特殊时候再加"="共64+1=65个字符,不明白为什么不叫Base65.经编码后的字符不会再出现其他的字符.ASCII码由8位(最高位为0,最小0,最大127)二进制数组成,而Base64要求将输入的字符串的每一位都化为其二进制形式,6位一分组(最大为63<64),这个6位二进制数的十进制值作为上面数组的下标,该下标对应的字符就是该所求字符对应Base64编码结果.例如字符串"rui",三个字符对应的ASCII码的二进制位(左高右低):011100100111010101101001六位一组:011100,100111,010101,101001对应的十进制数分别为:28,39,21,41.然后(数组下标是从0开始的)分别对应于 table[28]=c,table[39]=n,table[21]=V,table[41]=p.所以"rui"对应的Base64编码就是:cnVp;可以看出编码后的长度是原来长度的4/3倍.一个数字除以3,可余:0,1,2.余0的已经说过了,当分组后单下一个的时候(即余1的时候),就用该字符的高6位的十进制去查一次表,再用低2位去查一次表,最后不要忘了再在最后加上两个"="号,这是Base64编码的规定,例如:"core",分组后单下e,e对应的ASCII码为01100101,分为011001即:25,table[25]=Z此时剩下的01要先放再高位去,然后再补四个0,即:16,table[16]=Q,加上前面三个字符cor的编码结果Y29y,别忘了加上两个"=",最终"core"的 Base64编码结果为Y29yZQ==.当单下两个字符(即余2的时候),余下的两位共2*8=16个二进制位,前12位都可以直接查出他们对应的编码值,而剩下的4个比特位先将他们移到高位,后面补两个0,再加一个"=",例如"lirui",其中"lir"结果为"bGly","ui"的二进制为:01110101011001,前12位分别对应table[36]=d,table[22]=W.最后剩下的四位先移至高位,低两位补0,加上"lir"的编码为bGly,所以lirui的编码为bGlydWk=.

Base64编码常用于:SMTP协议中对登录用户名的验证,迅雷的下载地址等.

如下是C语言版的Base64编码:

#include
#include
#include
int in_len;
char *out_str;

char * base64_encodar (char *in_str)
{
    int i=0;
    char base64_table[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ

                                         abcdefghijklmnopqrstuvwxyz0123456789+/";   
    int curr_out=0;
if(in_len>0)
{
    while(i    {
        char a=in_str[i];   
        char b=i+1>=in_len?0:in_str[i+1];
        char c=i+2>=in_len?0:in_str[i+2];
        if(i+2        {
            out_str[curr_out++]=base64_table[(a>>2)&(0x3f)];         //the first
            out_str[curr_out++]=base64_table[((a&0x03)<<4)+(b>>4)];    //the first and the second
            out_str[curr_out++]=base64_table[((b&0x0f)<<2)+(c>>6)];    //the second and the third
            out_str[curr_out++]=base64_table[c&0x3f];           //the third
        }
        else if(i+1        {
            out_str[curr_out++]=base64_table[(a>>2)&(0x3f)];   //the first
            out_str[curr_out++]=base64_table[((a&0x03)<<4)+(b>>4)];  //the first and the second
            out_str[curr_out++]=base64_table[(b&0x0f)<<2];     //the last low 4 bits of the second
            out_str[curr_out++]='=';   //fill with the '='
        }
        else                          /*余1*/
        {
            out_str[curr_out++]=base64_table[(a>>2)&(0x3f)];  //the first
            out_str[curr_out++]=base64_table[(a&(0x03))<<4];  //the rest of of the first
            out_str[curr_out++]='=';    //fill with  '=' in the last two bits
            out_str[curr_out++]='=';
        }
        i+=3;
    }
      out_str[curr_out]='\0';
}
    return out_str;
};


int main(int argc,char **argv)
{
    char x[128];
    printf("Please input the String:\n");
    scanf("%s",x);
    in_len=strlen(x);
    out_str=(char*)malloc(4/3*in_len+1);
    strcpy(x,base64_encode(x));
    printf("the base64 encode is:\n%s\n",x);
    return 0;
}

阅读(1709) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~