/*** @brief C + + base64 codecs * @author wid* @date 2013-20-25** @note * * #include <iostream> #include <string> #inclu De <ctime>const char *enkey = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";//base64 Codec function declaration std::string b64encodestring (const std::string &strstring); The ASCII string is Base64 encoded std::string b64decodestring (const std::string &strstring); Decoding a base64 encoded string//BASE64 codec function implementation/*** @brief base64 encoding of ASCII strings * * @param strstring strings to be encoded * * @return srs::string Returns the encoded String * * @note for characters that contain non-ASCII strings in a string, the code throws an std::string type exception, capturing */std::string b64encodestring (const std::string & ; strstring) {int nbytesrc = Strstring.length (); std::string Pszsource = Strstring;int i = 0;for (i; i < nbytesrc; i++) if (Pszsource[i] < 0 | | pszsource[i] > 127) Throw "can not encode non-ascii characters"; std::string Pszencode (NBYTESRC * 4/3 + 4, ' + '); int nloop = nbytesrc% 3 = = 0? Nbytesrc:nbytesrc-3;int n = 0;for (i = 0; i < nloop; I + = 3) {Pszencode[n] = Enkey[pszsource[i] >> 2];p szencode[n + 1] = enkey[((Pszsource[i] & 3) << 4) | ((Pszsource[i + 1] & 0xF0) >> 4)]; Pszencode[n + 2] = enkey[((pszsource[i + 1] & 0x0f) << 2) | ((Pszsource[i + 2] & 0xc0) >> 6)]; Pszencode[n + 3] = Enkey[pszsource[i + 2] & 0x3f];n + = 4;} Switch (nbytesrc% 3) {case 0:pszencode[n] = '%1 '; break;case 1:pszencode[n] = Enkey[pszsource[i] >> 2];p szencode[n + 1] = enkey[((Pszsource[i] & 3) << 4) | ((0 & 0xf0) >> 4)]; Pszencode[n + 2] = ' = ';p szencode[n + 3] = ' = ';p szencode[n + 4] = ' + '; break;case 2:pszencode[n] = enkey[pszsource[i] >& Gt 2];p szencode[n + 1] = enkey[((Pszsource[i] & 3) << 4) | ((Pszsource[i + 1] & 0xf0) >> 4)]; Pszencode[n + 2] = enkey[((pszsource[i + 1] & 0xf) << 2) | ((0 & 0xc0) >> 6)]; Pszencode[n + 3] = ' = ';p szencode[n + 4] = ' + '; return Pszencode.c_str ();} /*** @brief decoding a base64 encoded string * * @param strstrinG String to decode * * @return std::string returns the decoded string * * @note decoding a non-Base64 encoded string or a corrupted base64 string throws a std::string exception, capture */STD:: String b64decodestring (const std::string &strstring) {int nbytesrc = Strstring.length (); std::string PszSource = strstring;const int dekey[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 1,-1, -1,62,//'/' 1, 1, -1,63, 55, 56, 57, 58, 59, 60, 61,//' 0 '-' 9 '-1,-1,-1,-1,-1,-1, -1,0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, +, 1, 1, -1,26, 27, 28, 29, 30, 31, 32, +---1,---------------+--------1, , Max, Total, Total, N, a, 0, 4, a, N, a ' a '-' Z '};if (nbytesrc%, ' = '), ' "Bad base64 string"; std::string Pszdecode (NBYTESRC * 3/4 + 4, ' pszsource[nbytesrc-1 '); int nloop =] = = ' = '? NByteSrc-4: nbytesrc;int B[4];int i = 0, n = 0;for (i = 0; i < nloop; I+ = 4) {b[0] = dekey[pszsource[i]]; B[1] = dekey[pszsource[i + 1]];b[2] = Dekey[pszsource[i + 2]]; B[3] = dekey[pszsource[i + 3]];if (b[0] = = 1 | | b[1] = = 1 | | b[2] = = 1 | | b[3] = = 1) throw "Bad base64 string";p szdecod E[n] = (B[0] << 2) | ((B[1] & 0x30) >> 4);p Szdecode[n + 1] = ((b[1] & 0xf) << 4) | ((B[2] & 0x3c) >> 2);p Szdecode[n + 2] = ((b[2] & 0x3) << 6) | B[3];n + = 3;} if (pszsource[nbytesrc-1] = = ' && pszsource[nbytesrc-2] = = ' = ') {b[0] = dekey[pszsource[i]]; B[1] = dekey[pszsource[i + 1]];if (b[0] = = 1 | | b[1] = = 1) throw "Bad base64 string";p szdecode[n] = (B[0] << 2) | ((B[1] & 0x30) >> 4);p Szdecode[n + 1] = ' + ';} if (pszsource[nbytesrc-1] = = ' = ' && pszsource[nbytesrc-2]! = ' = ') {b[0] = dekey[pszsource[i]]; B[1] = dekey[pszsource[i + 1]];b[2] = dekey[pszsource[i + 2]];if (b[0] = = 1 | | b[1] = = 1 | | b[2] = = 1) throw "bad base64 String ";p szdecode[n] = (B[0] << 2)| ((B[1] & 0x30) >> 4);p Szdecode[n + 1] = ((b[1] & 0xf) << 4) | ((B[2] & 0x3c) >> 2);p Szdecode[n + 2] = ' + ';} if (pszsource[nbytesrc-1]! = ' = ' && pszsource[nbytesrc-2]! = ' = ') pszdecode[n] = ' + '; return pszdecode;} int main () {////code Test std::string encodestr = "Ondragon"; std::cout << Ondragon encode base64: "<< B64encodestring (ENCODESTR) << std::endl;std::cout << std::endl;///decoding test std::string decodestr = B64encodestring (ENCODESTR); Std::cout << "to" << b64encodestring (ENCODESTR) << "base64 decode:" << B64decodestring (DECODESTR) << std::endl;std::cout << std::endl;system ("pause"); return 0;}
Base64/C + + implementation