Base64编解码原理

Base64编解码原理

Base64可以将ASCII字符串或者是二进制编码成只含A--Z,a--z,0--9,+,/ 这64个字符(这也是为啥叫Base64的原因).
 
1. 编码原理:将3个字符转换成4个字符( (3 X 8) = 24 字节= (4 X 6) )
   取输入流中的3个字节,每次取6位(共4次),以此6位值做索引去查表,输出相应的字符。如果输入流中字符不是3的倍数,最后结果缺的就以"="填充(输入流中最后只剩1个字符就填2个=;剩2个就填1个=).
base64digits[] =   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

           
2. 解码原理:将4个字符转换成3个字符.
            先读入输入流中的4个字符,然后根据上表中的值找索引(也可根据如下程序中的base64val[]索引查找),生成4个6位的值,然后以字节(8位)截断,就形成3个字符的ASCII值,这样就还原了.

C语言中是通过移位>> <<和按位&|实现的。
下面程序是网上的列子(有一个不正确的地方,偶稍微修改下了,并 加了测试)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <ctype.h>
 
 static const char base64digits[] =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
 #define BAD -1
 static const char base64val[] = {
     BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
     BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
     BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63,
      52, 53, 54, 55, 56, 57, 58, 59, 60, 61,BAD,BAD, BAD,BAD,BAD,BAD,
     BAD, 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,BAD, BAD,BAD,BAD,BAD,
     BAD, 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,BAD, BAD,BAD,BAD,BAD
 };
 #define DECODE64(c) (isascii(c) ? base64val[c] : BAD)
 
 void to64frombits(unsigned char *out, const unsigned char *in, int inlen)
 {
         for (; inlen >= 3; inlen -= 3)
         {
                 *out++ = base64digits[in[0] >> 2];
                 *out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)];
                 *out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
                 *out++ = base64digits[in[2] & 0x3f];
                 in += 3;
         }
 
         if (inlen > 0)
         {
                 unsigned char fragment;
 
                 *out++ = base64digits[in[0] >> 2];
                 fragment = (in[0] << 4) & 0x30;
 
                 if (inlen > 1)
                         fragment |= in[1] >> 4;
 
                 *out++ = base64digits[fragment];
                 *out++ = (inlen < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c];
                 *out++ = '=';
         }
         
         *out = '/0';
 }
 
 int from64tobits(char *out, const char *in)
 {
         int len = 0;
         register unsigned char digit1, digit2, digit3, digit4;
 
         if (in[0] == '+' && in[1] == ' ')
                 in += 2;
         if (*in == '/r')
                 return(0);
 
         do {
                 digit1 = in[0];
                 if (DECODE64(digit1) == BAD)
                         return(-1);
                 digit2 = in[1];
                 if (DECODE64(digit2) == BAD)
                         return(-1);
                 digit3 = in[2];
                 if (digit3 != '=' && DECODE64(digit3) == BAD)
                         return(-1);
                 digit4 = in[3];
                 if (digit4 != '=' && DECODE64(digit4) == BAD)
                         return(-1);
                 in += 4;
                 *out++ = (DECODE64(digit1) << 2) | (DECODE64(digit2) >> 4);
                 ++len;
                 if (digit3 != '=')
                 {
                         *out++ = ((DECODE64(digit2) << 4) & 0xf0) | (DECODE64(digit3) >> 2);
                         ++len;
                         if (digit4 != '=')
                         {
                                 *out++ = ((DECODE64(digit3) << 6) & 0xc0) | DECODE64(digit4);
                                 ++len;
                         }
                 }
         } while (*in && *in != '/r' && digit4 != '=');
        //don't forget this line!

         *out='/0';
 
         return (len);
 }

int main()
{

char in[]="linewer",*out,out2[8];
int inlen=strlen(in),n;

n=(inlen % 3)?(inlen/3+1)*4 : (inlen/3*4);
out=malloc(n+1);


to64frombits(out,in,inlen);
printf("%s/n",in);
printf("%s/n",out);
from64tobits(out2,out);
printf("%s/n",out2);

free(out);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值