Base64实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Base64 { public: static string Encrypt(string sPlainText); static string Decrypt(string sCipherText); private: static int ConvertToIndex(unsigned char ucChar); static string Algorithm(unsigned char ucByteHigh, unsigned char ucByteMiddle, unsigned char ucByteLow, unsigned char ucAvailableCount); static char s_caCharSet[64]; }; char Base64::s_caCharSet[64] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', '0','1','2','3','4','5','6','7','8','9', '+','/' }; int Base64::ConvertToIndex(unsigned char ucChar) { if (ucChar == '+') return 62; else if (ucChar == '/') return 63; else if (ucChar == '=') return 0; else if (ucChar >= 'A' && ucChar <= 'Z') { return ucChar - 65; } else if (ucChar >= 'a' && ucChar <= 'z') { return ucChar - 97 + 26; } else if (ucChar >= '0' && ucChar <= '9') { return ucChar - 48 + 26 + 26; } else { cerr<<"Base64 convert to Index failed"<<endl; return 0; // abort?? } } /** * Base64 convert algorithm(from 3 bytes to 4 bytes charset index) */ string Base64::Algorithm(unsigned char ucByteHigh, unsigned char ucByteMiddle, unsigned char ucByteLow, unsigned char ucAvailableCount) { string sResult; int iTemp = 0; unsigned char ucByte1, ucByte2, ucByte3, ucByte4; if (ucAvailableCount == 0) return ""; iTemp = ucByteHigh; iTemp <<= 8; iTemp |= ucByteMiddle; iTemp <<= 8; iTemp |= ucByteLow; ucByte4 = iTemp & 0x3F; ucByte3 = (iTemp & 0xFC0) >> 6; ucByte2 = (iTemp & 0x3F000) >> 12; ucByte1 = (iTemp & 0xFC0000) >> 18; if (ucAvailableCount == 1) { sResult += s_caCharSet[ucByte1]; sResult += s_caCharSet[ucByte2]; sResult += "=="; } else if (ucAvailableCount == 2) { sResult += s_caCharSet[ucByte1]; sResult += s_caCharSet[ucByte2]; sResult += s_caCharSet[ucByte3]; sResult += "="; } else if (ucAvailableCount == 3) { sResult += s_caCharSet[ucByte1]; sResult += s_caCharSet[ucByte2]; sResult += s_caCharSet[ucByte3]; sResult += s_caCharSet[ucByte4]; } else { return ""; } return sResult; } /** * sPlainText is plaintext that non encrypted * return Base64 encrypt text */ string Base64::Encrypt(string sPlainText) { string sResult; const unsigned int uPlainTextSize = sPlainText.size(); if (sPlainText.empty()) { cerr<<"invalid original text"<<endl; return ""; } if (uPlainTextSize == 1) { return Algorithm(sPlainText[0], 0, 0, 1); } else if (uPlainTextSize == 2) { return Algorithm(sPlainText[0], sPlainText[1], 0, 2); } else { unsigned int i; unsigned int uCount = uPlainTextSize / 3 * 3; for (i = 0; i < uCount; i+=3) { string sTmp = Algorithm(sPlainText[i], sPlainText[i+1], sPlainText[i+2], 3); sResult += sTmp; } unsigned int uMore = uPlainTextSize % 3; if ( uMore != 0) { if (uMore == 1) { sResult += Algorithm(sPlainText[i], 0, 0, 1); } else { sResult += Algorithm(sPlainText[i], sPlainText[i+1], 0, 2); } } return sResult; } } /** * input ciphertext and return related plaintext */ string Base64::Decrypt(string sCipherText) { string sResult; if (sCipherText.size() % 4 != 0) { cerr<<"Invalid parameter, Base64 can't convert from ciphertext to plaintext"<<endl; return ""; } // from high to low unsigned char ucByte1, ucByte2, ucByte3, ucByte4; for (unsigned int i = 0; i < sCipherText.size(); i+=4) { string sTmp; int iConvertTo3Bytes = 0; ucByte1 = ConvertToIndex(sCipherText[i]); ucByte2 = ConvertToIndex(sCipherText[i+1]); ucByte3 = ConvertToIndex(sCipherText[i+2]); ucByte4 = ConvertToIndex(sCipherText[i+3]); iConvertTo3Bytes = ucByte1; iConvertTo3Bytes <<= 6; iConvertTo3Bytes |= ucByte2; iConvertTo3Bytes <<= 6; iConvertTo3Bytes |= ucByte3; iConvertTo3Bytes <<= 6; iConvertTo3Bytes |= ucByte4; sTmp = (unsigned char)(iConvertTo3Bytes >> 16); unsigned char ucTmp = (unsigned char)((iConvertTo3Bytes & 0xFF00) >> 8); if (ucTmp == 0) { sResult += sTmp; continue; } else sTmp += ucTmp; ucTmp = (unsigned char)(iConvertTo3Bytes & 0xFF); if (ucTmp == 0) { sResult += sTmp; continue; } else sTmp += ucTmp; sResult += sTmp; } return sResult; }