Base64有什么用?
用记事本打开exe、jpg、pdf这些文件时,我们都会看到一大堆乱码,因为二进制文件包含很多无法显示和打印的字符。
所以,如果要让记事本这样的文本处理软件能处理二进制数据,或者使用json保存二进制信息,需要先把数据先做一个Base64编码,统统变成可见字符,再保存。
在Base64中的可打印字符包括大写英文字母A-Z、小写英文字母a-z、阿拉伯数字0-9,这样共有62个字符,此外两个可打印符号在不同的系统中而不同,通常用加号(+)和正斜杠(/)。外加“补全符号”,通常用等号(=)。
Base64是一种用64个字符来表示任意二进制数据的方法,常用于在URL、Cookie、网页中传输少量二进制数据。
Base64原理
Base64要求把每三个8Bit的字节转换为四个6Bit的字节(3*8 = 4*6 = 24),然后把6Bit再添两位高位0,组成四个8Bit的字节,也就是说,转换后的字符串理论上将要比原来的长1/3。
(原文)转换前 11111111, 11111111, 11111111 (二进制)
(Base64编码)转换后 00111111, 00111111, 00111111, 00111111 (二进制)
Qt中Base64的使用
//========>base64编码<========//
char src[] = "我爱C语言";
int src_len = strlen(src);//所需头文件#include <string.h>
//qDebug()所需头文件:#include <QDebug>
qDebug() << "1[我爱C语言] toBase64 = " << QByteArray(src, src_len).toBase64();
QString str = "我爱C语言";
QByteArray array;
array.append(str);
QByteArray base64 = array.toBase64();
qDebug() << "2[我爱C语言] toBase64 = " << base64;
char tmp[1024] = {0};
strcpy(tmp, base64.data());
qDebug() << "3[我爱C语言] toBase64 = " << tmp;
//========>base64解码<========//
QByteArray dst = QByteArray::fromBase64( tmp );
qDebug() << "dst = " << dst.data();
运行效果:
C语言中使用Base64
base64.h:
#ifndef BASE64_H
#define BASE64_H
//bindata编码成base64
//bindata: 源字符串
//binlength: 源字符串长度
//base64:目的字符串,base64字符串
//返回值:base64字符串
char * base64_encode( const unsigned char * bindata, int binlength, char * base64 );
//解码base64
//base64:源字符串
//bindata: 目的字符串
//返回值:目的字符串长度
int base64_decode( const char * base64, unsigned char * bindata );
#endif // BASE64_H
base64.c:
#include <string.h>
const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
//bindata编码成base64
//bindata: 源字符串
//binlength: 源字符串长度
//base64:目的字符串,base64字符串
//返回值:base64字符串
char * base64_encode( const unsigned char * bindata, int binlength, char * base64 )
{
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;
}
//解码base64
//base64:源字符串
//bindata: 目的字符串
//返回值:目的字符串长度
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;
}
main.c:
#include <stdio.h>
#include <string.h>
#include "base64.h"
int main()
{
//bindata编码成base64
//bindata: 源字符串
//binlength: 源字符串长度
//base64:目的字符串,base64字符串
//返回值:base64字符串
//char * base64_encode( const unsigned char * bindata, int binlength, char * base64 );
char src[] = "我爱C语言";
int src_len = strlen(src);
char base64[1024] = {0};
base64_encode(src, src_len, base64); //base64编码
printf("base64 = %s\n", base64);
memset(src, 0, sizeof(src) );
src_len = 0;
//解码base64
//base64:源字符串
//bindata: 目的字符串
//返回值:目的字符串长度
//int base64_decode( const char * base64, unsigned char * bindata );
src_len = base64_decode(base64, src);
printf("src = %s, strlen(src) = %lu, src_len = %d\n", src, strlen(src), src_len);
return 0;
}
运行效果:
本教程源码下载地址:http://download.csdn.net/detail/tennysonsky/9878452
参考资料:
1、https://www.zhihu.com/question/36306744/answer/71626823
2、http://www.cnblogs.com/yejianfei