CString Base64Encode (lpctstr lpszsrc) {assert (lpszsrc! = Null && afxisvalidstring (lpszsrc)); const char base64_encode_table [64] = {65, 66, 67, 68, 69, 70, 71, 72, // 00 - 07 73 , 74, 75, 76, 77, 78, 79, 80, // 08 - 15 81, 82, 83, 84, 85, 86, 87, 88, // 16 - 23 89, 90, 97, 98, 99 , 100, 101, 102, //24 - 31 103, 104, 105, 106, 107, 108, 109, 110, // 32 - 39 111, 112, 113, 114, 115, 116, 117, 118, / / 40 - 47 119, 120, 121, 122, 48, 49, 50, 51, // 48-55 52, 53, 54, 55, 56, 57, 43, 47}; // 56 - 63
Unsigned int ity;
LPCTSTSTR PINBUFFER = LPSZSRC;
INT nsize = (int) _tcslen (lpszsrc); char * poutbuffer = new char [nsize / 3 * 4 5]; ZeromeMory (poutbuffer, nsize / 3 * 4 5);
For (uint i = 0; i <_tcslen (lpszsrc) / 3; i ) {ITEST = (unsigned char) * Pinbuffer ; item = ity << 8;
ITEST = ITEST | (UNSIGNED Char) * Pinbuffer ; ITEST = ITEST << 8;
Item = iTest | (unsigned char) * Pinbuffer ;
// Reverse write output buffer Poutbuffer in 4 BYTE [3] = base64_encode_table [ITEST & 0X3F]; ITEST = ITEST >> 6; PoutBuffer [2] = Base64_ENCODE_TABLE [ITEST & 0X3F]; ITEST = ITEST >> 6; Poutbuffer [ 1] = Base64_encode_table [ITEST & 0x3F]; ITEST = ITEST >> 6; PoutBuffer [0] = Base64_encode_table [ity]; Poutbuffer = 4;
// Set the tail Switch (_Tcslen (LPSZSRC)% 3) {case 0: Break; case 1: item = (unsigned char) * pinbuffer; item = ity << 4; poutbuffer [1] = base64_encode_table [ITEST & 0X3F]; ITEST = ITEST >> 6; Poutbuffer [0] = Base64_encode_table [ITEST]; PoutBuffer [2] = '='; // Use '=' is the 64 yard fill the remaining portion PoutBuffer [3] = '='; Break; Case 2: iTest = (unsigned char) * pinbuffer ; item = ity | (unsigned char) * pinbuffer; item = ipest << 2; poutbuffer [2] = base64_encode_table [ITEST & 0x3F]; iTest = ITEST >> 6; PoutBuffer [1] = Base64_encode_table [ITEST & 0X3F]; ITEST = ITEST >> 6; PoutBuffer [0] = base64_encode_table [ity]; PoutBuffer [3] = '='; // Fill Remaining Byte. Break;} poutbuffer- = nsize / 3 * 4; cstring strencode = poutbuffer; delete [] poutbuffer; poutbuffer = null; return strencode;}
CString Base64Decode (LPCTSTR lpszSrc) {ASSERT (! LpszSrc = NULL && AfxIsValidString (lpszSrc)); const unsigned int BASE64_DECODE_TABLE [256] = {255, 255, 255, 255, 255, 255, 255, 255, // 00 - 07 255, 255, 255, 255, 255, 255, 255, 2555, 255, 255, 255, 2555555, 255, // 16 - 23 255, 255, 255, 255, 255, 255, 255, 255, // 24 - 31 255, 2555, 2555, 255, // 32-39 255, 255, 255, 62, 255, 255, 255, 63, // 40 - 47 52, 53, 54, 59, // 48 - 55 60, 61, 255, 2555, 255, 2555, 2555, // 56 - 63 255, 0 , 1, 2, 3, 4, 5, 6, //64 - 71 7, 8, 9, 10, 11, 12, 13, 14, // 72 - 79 15, 16, 17, 18, 19, 20 , 21, 22, // 80 - 87 23, 24, 255, 2555, // 88 - 95 255, 26, 27, 28, 29, 30, 31, 32, // 96 - 103 33, 34, 35, 36, 37, 38, 39, 40, // 104 - 111 41, 42, 43, 44, 45, 46, 47, 48, // 112 - 119 49, 50, 51, 255, 255, 255, 255, 255, // 120 - 127 255, 255, 255, 255, 255, 2555, 255, 255, 255, 255555, 255, 255, 255555, 255 , 255, 255, 255, 2555, 255, 255, 255, 255555, 255, 2555, 255555555, 255, 255, 255 , 255, 255, 255, 2555, 255, 255, 255, 255555, 255, 2555, 255555555, 255, 255, 255 , 255, 255, 255, 2555, 255, 255, 255, 255555, 255, 2555, 255555555, 255, 255, 255 , 255, 255, 255, 2555, 255, 255, 255, 255555, 255, 2555, 255555555, 255, 255, 255 , 255, 255, 255, 255};
const Int nsrccount = (int) _TCSLEN (LPSZSRC); int nsize = nsrccount / 4 * 3; if (lpszsrc [nsrccount-1] == '=') nsize--; IF (LPSZSRC [nsrcount-2] == ' = ') Nsize ---; char * poutbuffer = new char [nsize 3]; ZeromeMory (poutbuffer, nsize 3); lpctstr pinbuffer = lpszsrc; uint itest, iPack; for (int i = 0; i