基于C++的Base64编解码实现

//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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

princewwj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值