//base64.cpp
//
#include <string>
#include <stdio.h>
#include <string.h>
#include <iostream>
std::string base64encode(const char *infile,const char *outfile)
{
int DataByte=0;
unsigned char* Data=NULL;
FILE *fp=fopen(infile,"rb");
if(fp==NULL)
{
return "\0";
}
fseek(fp,0L,SEEK_END);
DataByte=ftell(fp);
Data=new unsigned char[DataByte+1];
fseek(fp,0L,SEEK_SET);
fread(Data,DataByte,1,fp);
fclose(fp);
//code table
const char EncodeTable[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
//return value
std::string strEncode;
unsigned char Tmp[4]={0};
for(int i=0;i<(int)(DataByte/3);i++)
{
Tmp[1]=*Data++;
Tmp[2]=*Data++;
Tmp[3]=*Data++;
strEncode+=EncodeTable[Tmp[1]>>2];
strEncode+=EncodeTable[((Tmp[1]<<4) | (Tmp[2]>>4)) & 0x3F];
strEncode+=EncodeTable[((Tmp[2]<<2) | (Tmp[3]>>6)) & 0x3F];
strEncode+=EncodeTable[Tmp[3] & 0x3F];
}
//encode the remaining bytes
int Mod=DataByte % 3;
if(Mod==1)
{
Tmp[1]=*Data++;
strEncode+=EncodeTable[(Tmp[1] & 0xFC)>>2];
strEncode+=EncodeTable[((Tmp[1] & 0x03)<<4)];
strEncode+="==";
}
else if(Mod==2)
{
Tmp[1]=*Data++;
Tmp[2]=*Data++;
strEncode+=EncodeTable[(Tmp[1] & 0xFC)>>2];
strEncode+=EncodeTable[((Tmp[1] & 0x03)<<4) | ((Tmp[2] & 0xF0)>>4)];
strEncode+=EncodeTable[((Tmp[2] & 0x0F)<<2)];
strEncode+="=";
}
//std::cout<<strEncode;
fp=fopen(outfile,"wb+");
fwrite(strEncode.c_str(),strEncode.size(),1,fp);
fclose(fp);
delete[] Data;
Data=NULL;
return strEncode;
}
std::string base64decode(const char *infile,const char *outfile)
{
int DataByte=0;
unsigned char* Data=NULL;
FILE *fp=fopen(infile,"rb");
if(fp==NULL)
{
return "\0";
}
fseek(fp,0L,SEEK_END);
DataByte=ftell(fp);
Data=new unsigned char[DataByte+1];
fseek(fp,0L,SEEK_SET);
fread(Data,DataByte,1,fp);
fclose(fp);
std::string strString=(char*)Data;
delete[] Data;
Data=NULL;
int nByteSrc=strString.length();
std::string pszSource=strString;
const int dekey[]=
{
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
62, // '+'
-1, -1, -1,
63, // '/'
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
-1, -1, -1, -1, -1, -1, -1,
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, // 'A'-'Z'
-1, -1, -1, -1, -1, -1,
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, // 'a'-'z'
};
if(nByteSrc%4 !=0)
{
throw "bad base64 string";
}
std::string pszDecode(nByteSrc*3/4+4, '\0');
int nLoop=pszSource[nByteSrc-1] =='=' ? nByteSrc - 4 : nByteSrc;
int b[4]={0};
int i=0, n=0;
for(i=0; i < nLoop; i +=4 )
{
b[0]=dekey[pszSource[i]];
b[1]=dekey[pszSource[i+1]];
b[2]=dekey[pszSource[i+2]];
b[3]=dekey[pszSource[i+3]];
if(b[0]==-1 || b[1]==-1 || b[2]==-1 || b[3]==-1)
{
throw "bad base64 string";
}
pszDecode[n]=(b[0]<<2) | ((b[1] & 0x30)>>4);
pszDecode[n+1]=((b[1] & 0xf)<<4) | ((b[2] & 0x3c)>>2);
pszDecode[n+2]= ((b[2] & 0x3)<<6) | b[3];
n+=3;
}
if( pszSource[nByteSrc-1]=='=' && pszSource[nByteSrc-2]=='=' )
{
b[0]=dekey[pszSource[i]];
b[1]=dekey[pszSource[i+1]];
if(b[0]==-1 || b[1]==-1)
{
throw "bad base64 string";
}
pszDecode[n]=(b[0]<<2) | ((b[1] & 0x30)>>4);
pszDecode[n+1]='\0';
}
if( pszSource[nByteSrc-1]=='=' && pszSource[nByteSrc-2] !='=' )
{
b[0]=dekey[pszSource[i]];
b[1]=dekey[pszSource[i+1]];
b[2]=dekey[pszSource[i+2]];
if(b[0]==-1 || b[1]==-1 || b[2]==-1)
{
throw "bad base64 string";
}
pszDecode[n]=(b[0]<<2) | ((b[1] & 0x30)>>4);
pszDecode[n+1]=((b[1] & 0xf)<<4) | ((b[2] & 0x3c)>>2);
pszDecode[n+2]='\0';
}
if( pszSource[nByteSrc-1] !='=' && pszSource[nByteSrc-2] !='=' )
{
pszDecode[n]='\0';
}
fp=fopen(outfile,"wb+");
for(i=0;i<n;i++)
{
fwrite(&pszDecode[i],1,1,fp);
}
fclose(fp);
return pszDecode;
}
int main(int argc,char *argv[])
{
if(argc<1+1)
{
std::cout<<argv[0]<<" "<<"enc[dec]"<<" "<<"inputfile"<<" "<<"outputfile"<<std::endl;
return -1;
}
if(strcmp(argv[1],"enc")==0)
{
base64encode(argv[2],argv[3]);
}
else if(strcmp(argv[1],"dec")==0)
{
base64decode(argv[2],argv[3]);
}
return 0;
}
编译命令如下:
g++ -std=c++17 -o encdec.exe Base64.cpp
调用格式如下:
encdec.exe enc[dec] inputfile outputfile