在网络上看到了,感觉不错,特发来分享
//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 char str_pad = '=';//pad 用“=”标记
//内部申请内存,外部一定要记得释放内存
char *base64_encode(char *str, int length)
{
char *result,*p;
if(str == NULL || length <1)return NULL;
//分配空间,加密后的字符串长度是原字符串的4/3
result = (char *)malloc(((length + 2)/3)*4);
p = result;
if(result == NULL)
{
printf("malloc failed\n");
return NULL;
}
//这是剩余字符串长度大于等于3的情况
//通过位移来截取字节的位数
//第一个字节右移2位,得到base64的第一个目标字符
//第一个字节&0x03(00000011)后,再加上第二个字节右移4位,得到base64的第二个目标字符
//第二个字节&0x0f (00001111) 后,再加上第三个字节右移4位,得到base64的第三个目标字符
//第三个字节&0x3f (00111111) 后,得到base64的第四个目标字符
while( length > 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];
length -= 3;
str += 3;
}
//剩余字符串长度小于3
if(length != 0)
{
*result++ = base64_table[str[0] >> 2];
//剩余长度为2
if(length > 1)
{
*result++ = base64_table[((str[0] & 0x03) << 4) + (str[1] >> 4)];
*result++ = base64_table[(str[1] & 0x0f) << 2];
*result++ = str_pad; //不够的补"="
}
//剩余字符串长度是1
else
{
*result++ = base64_table[(str[0] & 0x03) << 4];
*result++ = str_pad;//不够的补"="
*result++ = str_pad;//不够的补"="
}
}
*result ='\0';
//输出结果
//printf("result:%s\n",p);
return p;
}
/* Base-64 decoding. This represents binary data as printable ASCII
** characters. Three 8-bit binary bytes are turned into four 6-bit
** values, like so:
**
** [11111111] [22222222] [33333333]
**
** [111111] [112222] [222233] [333333]
**
** Then the 6-bit values are represented using the characters "A-Za-z0-9+/".
*/
static int b64_decode_table[256] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
};
/* Do base-64 decoding on a string. Ignore any non-base64 bytes.
** Return the actual number of bytes generated. The decoded size will
** be at most 3/4 the size of the encoded, and may be smaller if there
** are padding characters (blanks, newlines).
*/
int base64_decode( const char* str, unsigned char* space, int size )
{
const char* cp = NULL;
int space_idx = 0, phase = 0;
int d = 0, prev_d = 0;
unsigned char c;
space_idx = 0;
phase = 0;
for ( cp = str; *cp != '\0'; ++cp )
{
d = b64_decode_table[(int)*cp];
if ( d != -1 )
{
switch ( phase )
{
case 0:
++phase;
break;
case 1:
c = ( ( prev_d << 2 ) | ( ( d & 0x30 ) >> 4 ) );
if ( space_idx < size )
space[space_idx++] = c;
++phase;
break;
case 2:
c = ( ( ( prev_d & 0xf ) << 4 ) | ( ( d & 0x3c ) >> 2 ) );
if ( space_idx < size )
space[space_idx++] = c;
++phase;
break;
case 3:
c = ( ( ( prev_d & 0x03 ) << 6 ) | d );
if ( space_idx < size )
space[space_idx++] = c;
phase = 0;
break;
}
prev_d = d;
}
}
return space_idx;
}
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)