zoukankan      html  css  js  c++  java
  • c++ base64 class

    #ifndef _ZBASE64
    #define _ZBASE64
    
    #pragma warning(disable:4786) 
    #include <string>
    using namespace std;
    
    class ZBase64
    {
    
    private:
    
    //Base64编码解码表
    char* m_Base64_Table;
    
    public:
    
    //构造
    ZBase64();
    
    //编码
    string EncodeBase64(const string strSource);
    
    //解码
    string DecodeBase64(const string strSource);
    };
    #endif
    #include "http://www.cnblogs.com/src/utility/base64.h"
    
    
    
    ZBase64::ZBase64()
    {
    //Base64编码表
    this->m_Base64_Table="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    }
    
    string ZBase64::EncodeBase64(const string strSource)
    {
    /*
    * 以下是操作二进制数时用到的
    * 11111100   0xFC
    * 11000000   0x3
    * 11110000   0xF0
    * 00001111   0xF
    * 11000000   0xC0
    * 00111111   0x3F
    */
    
    string strEncode;
    char cTemp[4];
    
    //行长,MIME规定Base64编码行长为76字节
    int LineLength=0;
    
    for(int i=0;i<strSource.size();i+=3)
    {
       memset(cTemp,0,4);
    
       cTemp[0]=strSource[i];
       cTemp[1]=strSource[i+1];
       cTemp[2]=strSource[i+2];
    
       int len=strlen(cTemp);
       if(len==3)
       {
        strEncode+=this->m_Base64_Table[((int)cTemp[0] & 0xFC)>>2];
        strEncode+=this->m_Base64_Table[((int)cTemp[0] & 0x3)<<4 | ((int)cTemp[1] & 0xF0)>>4];
        strEncode+=this->m_Base64_Table[((int)cTemp[1] & 0xF)<<2 | ((int)cTemp[2] & 0xC0)>>6];
        strEncode+=this->m_Base64_Table[(int)cTemp[2] & 0x3F];
        if(LineLength+=4>=76) strEncode+="\r\n";
       }
       else if(len==2)
       {
        strEncode+=this->m_Base64_Table[((int)cTemp[0] & 0xFC)>>2];
        strEncode+=this->m_Base64_Table[((int)cTemp[0] & 0x3 )<<4 | ((int)cTemp[1] & 0xF0 )>>4];
        strEncode+=this->m_Base64_Table[((int)cTemp[1] & 0x0F)<<2];
        if(LineLength+=4>=76) strEncode+="\r\n";
        strEncode+="=";
       }
       else if(len==1)
       {
        strEncode+=this->m_Base64_Table[((int)cTemp[0] & 0xFC)>>2];
        strEncode+=this->m_Base64_Table[((int)cTemp[0] & 0x3 )<<4];
        if(LineLength+=4>=76) strEncode+="\r\n";
        strEncode+="==";
       }
       memset(cTemp,0,4);
    }
    return strEncode;
    }
    
    string ZBase64::DecodeBase64(const string strSource)
    {
    //返回值
    string strDecode;
    char cTemp[5];
    
    for(int i=0;i<strSource.size();i+=4)
    {
       memset(cTemp,0,5);
    
       cTemp[0]=strSource[i];
       cTemp[1]=strSource[i+1];
       cTemp[2]=strSource[i+2];
       cTemp[3]=strSource[i+3];
    
       int asc[4];
       for(int j=0;j<4;j++)
       {
        for(int k=0;k<(int)strlen(this->m_Base64_Table);k++)
        {
         if(cTemp[j]==this->m_Base64_Table[k]) asc[j]=k;
        }
       }
       if('='==cTemp[2] && '='==cTemp[3])
       {
        strDecode+=(char)(int)(asc[0] << 2 | asc[1] << 2 >> 6);
       }
       else if('='==cTemp[3])
       {
        strDecode+=(char)(int)(asc[0] << 2 | asc[1] << 2 >> 6);
        strDecode+=(char)(int)(asc[1] << 4 | asc[2] << 2 >> 4);
       }
       else
       {
        strDecode+=(char)(int)(asc[0] << 2 | asc[1] << 2 >> 6);
        strDecode+=(char)(int)(asc[1] << 4 | asc[2] << 2 >> 4);
        strDecode+=(char)(int)(asc[2] << 6 | asc[3] << 2 >> 2);
       }  
    }
    return strDecode;
    }


    //调用方式

    ZBase64 base64;

    string eccode=base64.EncodeBase64("hello");

    //输出结果为:aGVsbG8=

    string decode=base64.DecodeBase64("aGVsbG8=");


    printf("%s",eccode.c_str());

    //输出结果为:hello


        system("pause");
  • 相关阅读:
    Android热补丁动态修复
    Android用gif做启动页
    如果通过adb查看当前显示的activity
    Android网络框架比较
    Android高清巨图加载方案
    一些Windows下经验
    工作总结
    C++网络编程总结
    编写导出库
    GridCtrl控件的使用
  • 原文地址:https://www.cnblogs.com/ytjjyy/p/2487191.html
Copyright © 2011-2022 走看看