今天看到了base64算法,这个算法应该都耳熟能详了,经常会听到。谷歌了一下看了一下算法原理和这个算法产生的历史原因。base64编码产生的原因是说不想让Email的内容在互联网上明文传输,于是就想出了这种加密方法,但是我想这不是单向加密,得到密文之后懂解密算法的人还是可以得到原文的,所以说这种加密只是用来应付普通大众,不知道这种说法对不对。
贴出一些介绍的文章出来。
BASE64编码在C#、Delphi和JavaScript中的实现
浅谈Base64编码
自己也是了一下去实现,参考的是PHP里面的base64_encode函数实现方式。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//首先需要一个数组存储base64的字符表:
static char base64_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', '+', '/', '\0'
};
//接着是解码的时候要用到的字符反映射表,主要作用与上面的相反,上面的字符表是根据索引得出字符,下面的表是根据字符得出索引
static int base64_reverse_table[127] = {
-2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -1, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, 62, -2, -2, -2, 63, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, -2, -2, -2, -2, -2, -2, -2, 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, -2, -2, -2, -2, -2,-2, 26, 27,
8, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
42,43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2
};
//用于充字符的
static char str_pad = '=';
//base64加密函数
char *base64_encode(char *str, int len)
{
char *result,*p;
if(str == NULL || len <1)return NULL;
//分配空间,加密后的字符串长度是原字符串的4/3
result = (char *)malloc(((len + 2) / 3) * 4);
p = result;
if(result == NULL){
printf("malloc failed\n");
return NULL;
}
//加密函数的关键,把三个字符为一组的串拆成四个字符
//这是字符串长度大于等于3的情况,不需要填充符
while( len > 2)
{
//通过位移很巧妙的实现字符串的拆分,这一部分很值得回味和学习
*result++ = base64_table[str[0] >> 2];
*result++ = base64_table[((str[0] & 0x03) << 4) + (str[1] >> 4)];
*result++ = base64_table[((str[1] & 0x0f) << 2) + (str[2] >> 6)];
*result++ = base64_table[str[2] & 0x3f];
len -= 3;
str += 3;
}
//字符串长度小于3
if(len != 0)
{
*result++ = base64_table[str[0] >> 2];
//剩余长度为2
if(len > 1)
{
*result++ = base64_table[((str[0] & 0x03) << 4) + (str[1] >> 4)];
*result++ = base64_table[(str[1] & 0x0f) << 2];
*result++ = str_pad;
}else{
*result++ = base64_table[(str[0] & 0x03) << 4];
*result++ = str_pad;
*result++ = str_pad;
}
}
*result = '\0';
//打印结果看看.
printf("result:%s\n",p);
return p;
}
//解密算法,解密算法的思路就是把每四个一组的字符拆成三个字节
static char *base64_decode(char *str, int len)
{
int i=0,j=0;
char *result,*p;
int ch;
if(str == NULL || len < 0)return NULL;
//分配空间
result = (char *)malloc((len / 4) * 3);
p = result;
if(result == NULL){
printf("malloc failed\n");
return NULL;
}
//解密算法开始工作
while((ch = *str++) != '\0' && length-- > 0)
{
//遇到填充符
if(ch == str_pad)break;
//根据字符取得字符在base64_table里面的索引值
ch = base64_reverse_table[ch];
switch(i % 4)
{
case 0 : result[j] = ch << 2; break;
case 1 : result[j++] |= ch >> 4;
result[j] = (ch & 0x0f) << 4;
break;
case 2 : result[j++] |= ch >> 2;
result[j] = (ch & 0x03) << 6;
break;
case 3 : result[j++] |= ch;break;
}
i++;
}
result[j] = '\0';
//打印结果看看.
printf("result:%s\n",p);
return p;
}
int main(int c, char *v[]){
char *str,*result;
int len;
str = v[1];
len = strlen(str);
result = base64_encode(str, len);
len = strlen(result);
result = base64_decode(result, len);
return 1;
}


算法高手啊
强烈谴责还将这种算法叫做“加密算法”。 另外,貌似base64的初衷并不是为了让别人看不明白,而是为了满足信件中非英文字符的要求。
呵呵,大风。 刚查了一下wiki,好像确实是用来解决字符问题。 The particular choice of base is due to the history of character set encoding: one can choose a set of 64 characters that is both part of the subset common to most encodings, and also printable.
一直报段错误,,有点郁闷阿
呵呵,段错误基本都是指针越界或者没有初始化造成的。可以查看一下那些指针变量,可以用gdb调试看看。
好的
额,忘在文章里加您的blog地址了,大部分是参照您的