base64的解码与编码
Base64是一种以64个可打印字符对二进制数据进行编码算,base64在对数据进行编码时以三个8位字符型数据有六位,所以他的最大值位二的六次方为六十四,所以我们以4个6位数的十进制数base64表中得到最终编码后的字符。
编码表
由于base64编码是将编码前的3*8位数据,分解成4个6位的数据,所以经过base64编码后的字符串长度是4的倍数。但往往我们进行编码的数据长度并不是3的倍数,这就造成了“编码”后的位数不为4的倍数,比如Brisk共5×8=40位,以6位为一组可以分为7组,这样“编码”后就有7个字符,但base64编码后的字符长度应该是4的倍数,显然这里就出问题了,那么怎么办呢?前面的不可以抛弃掉,所以就只有“追加”了,所以Brisk经过base64编码后的长度应该是8个字符,而第8个编码后的字符是'=',再比如对单个字符a进行base64编码,由于它的长度不是3的倍数,以3个字节为一组它只能分一组,再以6位为一位它只能分两组,所以经过“编码”后它的长度是2,但base64编码后的个数应该是4的倍数,所以它的长度应该是4,所以在后面补上两个‘=’,由于一个数求余3后有三个不同的结果,0、1、2,所以在对一个数据进行base64进行编码后它的长度为:
(1)当进行编码的数据长度是3的倍数时,len=strlen(str_in)/3*4;
(2)当进行编码的数据长度不是3的倍数时,len=(strlen(str_in)/3+1)*4;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <io.h>
#include <fcntl.h>
#include <stdbool.h>
#ifndef MAX_PATH
#define MAX_PATH 256
#endif
const char * base64char = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”;
char * base64_encode( const unsigned char * bindata, char * base64, int binlength )
{
int i, j;
unsigned char current;
for ( i = 0, j = 0 ; i < binlength ; i += 3 )
{
current = (bindata[i] >> 2) ;
current &= (unsigned char)0x3F;
base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i] << 4 ) ) & ( (unsigned char)0x30 ) ;
if ( i + 1 >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+1] >> 4) ) & ( (unsigned char) 0x0F );
base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i+1] << 2) ) & ( (unsigned char)0x3C ) ;
if ( i + 2 >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+2] >> 6) ) & ( (unsigned char) 0x03 );
base64[j++] = base64char[(int)current];
current = ( (unsigned char)bindata[i+2] ) & ( (unsigned char)0x3F ) ;
base64[j++] = base64char[(int)current];
}
base64[j] = '\0';
return base64;
}
int base64_decode( const char * base64, unsigned char * bindata )
{
int i, j;
unsigned char k;
unsigned char temp[4];
for ( i = 0, j = 0; base64[i] != ‘\0’ ; i += 4 )
{
memset( temp, 0xFF, sizeof(temp) );
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i] )
temp[0]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+1] )
temp[1]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+2] )
temp[2]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+3] )
temp[3]= k;
}
bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2))&0xFC)) |
((unsigned char)((unsigned char)(temp[1]>>4)&0x03));
if ( base64[i+2] == '=' )
break;
bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4))&0xF0)) |
((unsigned char)((unsigned char)(temp[2]>>2)&0x0F));
if ( base64[i+3] == '=' )
break;
bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6))&0xF0)) |
((unsigned char)(temp[3]&0x3F));
}
return j;
}
void encode(FILE * fp_in, FILE * fp_out)
{
unsigned char bindata[2050];
char base64[4096];
size_t bytes;
while ( !feof( fp_in ) )
{
bytes = fread( bindata, 1, 2049, fp_in );
base64_encode( bindata, base64, bytes );
fprintf( fp_out, “%s”, base64 );
}
}
void decode(FILE * fp_in, FILE * fp_out)
{
int i;
unsigned char bindata[2050];
char base64[4096];
size_t bytes;
while ( !feof( fp_in ) )
{
for ( i = 0 ; i < 2048 ; i ++ )
{
base64[i] = fgetc(fp_in);
if ( base64[i] == EOF )
break;
else if ( base64[i] == ‘\n’ || base64[i] == ‘\r’ )
i --;
}
bytes = base64_decode( base64, bindata );
fwrite( bindata, bytes, 1, fp_out );
}
}
void help(const char * filepath)
{
fprintf( stderr, “Usage: %s [-d] [input_filename] [-o output_filepath]\n”, filepath );
fprintf( stderr, “\t-d\tdecode data\n” );
fprintf( stderr, “\t-o\toutput filepath\n\n” );
}
int main(int argc, char * argv[])
{
FILE * fp_input = NULL;
FILE * fp_output = NULL;
bool isencode = true;
bool needHelp = false;
int opt = 0;
char input_filename[MAX_PATH] = “”;
char output_filename[MAX_PATH] = “”;
opterr = 0;
while ( (opt = getopt(argc, argv, "hdo:")) != -1 )
{
switch(opt)
{
case 'd':
isencode = false;
break;
case 'o':
strncpy(output_filename, optarg, sizeof(output_filename));
output_filename[sizeof(output_filename)-1] = '\0';
break;
case 'h':
needHelp = true;
break;
default:
fprintf(stderr, "%s: invalid option -- %c\n", argv[0], optopt);
needHelp = true;
break;
}
}
if ( optind < argc )
{
strncpy(input_filename, argv[optind], sizeof(input_filename));
input_filename[sizeof(input_filename)-1] = '\0';
}
if (needHelp)
{
help(argv[0]);
return EXIT_FAILURE;
}
if ( !strcmp(input_filename, "") )
{
fp_input = stdin;
if (isencode)
_setmode( _fileno(stdin), _O_BINARY );
}
else
{
if (isencode)
fp_input = fopen(input_filename, "rb");
else
fp_input = fopen(input_filename, "r");
}
if ( fp_input == NULL )
{
fprintf(stderr, "Input file open error\n");
return EXIT_FAILURE;
}
if ( !strcmp(output_filename, "") )
{
fp_output = stdout;
if (!isencode)
_setmode( _fileno(stdout), _O_BINARY );
}
else
{
if (isencode)
fp_output = fopen(output_filename, "w");
else
fp_output = fopen(output_filename, "wb");
}
if ( fp_output == NULL )
{
fclose(fp_input);
fp_input = NULL;
fprintf(stderr, "Output file open error\n");
return EXIT_FAILURE;
}
if (isencode)
encode(fp_input, fp_output);
else
decode(fp_input, fp_output);
fclose(fp_input);
fclose(fp_output);
fp_input = fp_output = NULL;
return EXIT_SUCCESS;
}