Base64實現,base64

來源:互聯網
上載者:User

Base64實現,base64

 

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;}View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.