| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
10年前发布

C++分割文件并进行BASE64编码

#include <string>  using namespace std;  string Encode(char* Data,long DataByte)  {      //编码表      const char EncodeTable[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";      //返回值      string strEncode;      unsigned char Tmp[4]={0};      for(long i=0;i<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];      }      //对剩余数据进行编码      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+= "=";      }      return strEncode;  }          CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("All Files(*.*)|*.*|"));          if(dlg.DoModal()==IDOK)          {              CFile m_splitFile;              if(m_splitFile.Open(dlg.GetPathName(),CFile::modeRead | CFile::shareDenyWrite))              {                  CString targetPath="D:";                  CString m_fileTitle=m_splitFile.GetFileName();                  DWORD FileLength=m_splitFile.GetLength();                  DWORD PartLength=1024*512;                  int nCount=1;                  CString strName;                  CFile wrFile;                  DWORD ReadBytes;                  char* pBuf=new char[PartLength];                  while((ReadBytes=m_splitFile.Read(pBuf,PartLength))>=PartLength)                  {                      strName.Format("%s\\%s%d.enc",targetPath,m_fileTitle,nCount++);                      wrFile.Open(strName,CFile::modeWrite | CFile::modeCreate);                      string encStr=Encode(pBuf,ReadBytes);                      wrFile.Write(encStr.c_str(),encStr.size());                      wrFile.Flush();                      wrFile.Close();                  }                  if(nCount==1 && ReadBytes<PartLength)                  {                      strName.Format("%s\\%s.enc",targetPath,m_fileTitle,nCount++);                      wrFile.Open(strName,CFile::modeWrite | CFile::modeCreate);                      string encStr=Encode(pBuf,ReadBytes);                      wrFile.Write(encStr.c_str(),encStr.size());                      wrFile.Flush();                      wrFile.Close();                  }                  m_splitFile.Close();                  delete pBuf;              }          }