編碼:編碼
std::string CCryptoEnvelop::Base64EncryptStr(byte* pPlaintext, int nLen) { std::string outstr; if (NULL == pPlaintext || nLen < 1) return outstr; Base64Encoder encoder; encoder.Put(pPlaintext, nLen); encoder.MessageEnd(); word64 size = encoder.MaxRetrievable(); if(size) { outstr.resize(size); encoder.Get((byte*)outstr.data(), size); } return outstr; } //路徑能夠是png,word,excel格式的文件 CString COnePictureOneTable::GetBase64ToPath(const CString& strPath ) { FILE*pFile = NULL; fopen_s(&pFile, CComVar(strPath).ToString().c_str(), "rb"); if (pFile == NULL) return _T(""); fseek(pFile, 0x0L, SEEK_END); long size = ftell(pFile);//文件大小單位爲字節(B),1MB = 1024KB,1KB = 1024B byte *buf = new byte[size]; fseek(pFile, 0x0L, SEEK_SET); fread(buf, sizeof(byte), size, pFile); fclose(pFile); CString strBase64 = CCryptoEnvelop::Base64EncryptStr(buf, size).c_str(); delete[] buf; buf = NULL; return strBase64; }
解碼:excel
std::string CCryptoEnvelop::Base64EncryptStr(std::string plaintext) { std::string outstr; Base64Encoder encoder; encoder.Put((byte*)plaintext.c_str(), plaintext.length()); encoder.MessageEnd(); word64 size = encoder.MaxRetrievable(); if(size) { outstr.resize(size); encoder.Get((byte*)outstr.data(), outstr.size()); } return outstr; }