📄 base64.h
字号:
/************************************************
* *
* CBase64.h *
* Base 64 de- and encoding class *
* *
* ============================================ *
* *
* This class was written on 28.05.2003 *
* by Jan Raddatz [jan-raddatz@web.de] *
* *
* ============================================ *
* *
* Copyright (c) by Jan Raddatz *
* This class was published @ codeguru.com *
* 28.05.2003 *
* *
************************************************/
/*----------------------------------------------------------------------------------
@ Base64编码相关
@ Base64是MIME邮件中常用的编码方式之一。它的主要思想是将输入的字符串或数据编码成只含
有{'A'-'Z', 'a'-'z', '0'-'9', '+', '/'}这64个可打印字符的串,故称为“Base64”。
@ Base64编码的方法是,将输入数据流每次取6 bit,用此6 bit的值(0-63 ; 2^6 = 64)作为索
引去查表,输出相应字符。这样,每3个字节将编码为4个字符(3×8 → 4×6);不满4个字符
的以'='填充。
@ 解码时注意:如果对一个编码字符串分多次解码,则需要每次的字符数为4的整数倍。
----------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------
// for test
CBase64 m64;
CString strSour, strDest;
CString strFileSour, strFileDest, strFileSwap;
strFileSour = _T("c:\\zxt\\online\\EncodeBufftoFile.txt");
strFileDest = _T("c:\\zxt\\online\\DecodeBufftoFile.txt");
strSour = _T("004abcd");
strDest = _T("1tDQy82ovK/NxTIwMDRhYmNk");
// 测试通过 2004-09-07
// 将字符串编码为文件
m64.EncodeBufftoFile((char *)(LPCTSTR)strSour,
strSour.GetLength()*sizeof(TCHAR),
(LPTSTR)(LPCTSTR)strFileDest);
// 将字符串解码为文件
m64.DecodeBufftoFile((char *)(LPCTSTR)strDest,
(LPTSTR)(LPCTSTR)strFileSour);
// 测试通过 2004-09-07
strFileSour = _T("c:\\zxt\\online\\EncodeFile.txt");
strFileSwap = _T("c:\\zxt\\online\\EncodeSwapFile.txt");
strFileDest = _T("c:\\zxt\\online\\DecodeFile.txt");
// 对文件进行编码
m64.EncodeFile((LPTSTR)(LPCTSTR)strFileSour, (LPTSTR)(LPCTSTR)strFileSwap);
// 对文件进行解码
m64.DecodeFile((LPTSTR)(LPCTSTR)strFileSwap, (LPTSTR)(LPCTSTR)strFileDest);
char buf[100];
char buff[100];
memset(buf, 0, 100);
memset(buff, 0, 100);
// 通过2004-09-07
// 对字符串进行编码
m64.EncodeBuffer((char *)(LPCTSTR)strSour,
strSour.GetLength()*sizeof(TCHAR),
buf);
// 对字符串进行解码
CString strtemp = buf;
m64.DecodeBuffer((char *)(LPCTSTR)strtemp, buff);
CString strtest = buff;
MessageBox(strtest);
--------------------------------------------------------------------------------------*/
#pragma once
#include <afx.h>
#include <stdlib.h>
#include <math.h>
#include <memory.h>
const static unsigned int MAX_LINE_LENGTH = 76;
const static char BASE64_ALPHABET [64] =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', // 0 - 9
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', // 10 - 19
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', // 20 - 29
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 30 - 39
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', // 40 - 49
'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', // 50 - 59
'8', '9', '+', '/' // 60 - 63
};
const static char BASE64_DEALPHABET [128] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 9
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10 - 19
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20 - 29
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30 - 39
0, 0, 0, 62, 0, 0, 0, 63, 52, 53, // 40 - 49
54, 55, 56, 57, 58, 59, 60, 61, 0, 0, // 50 - 59
0, 61, 0, 0, 0, 0, 1, 2, 3, 4, // 60 - 69
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 70 - 79
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 80 - 89
25, 0, 0, 0, 0, 0, 0, 26, 27, 28, // 90 - 99
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // 100 - 109
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, // 110 - 119
49, 50, 51, 0, 0, 0, 0, 0 // 120 - 127
};
enum
{
UNABLE_TO_OPEN_INPUT_FILE,
UNABLE_TO_OPEN_OUTPUT_FILE,
UNABLE_TO_CREATE_OUTPUTBUFFER
};
class CBase64
{
private:
unsigned int CalculateRecquiredEncodeOutputBufferSize (unsigned int p_InputByteCount);
unsigned int CalculateRecquiredDecodeOutputBufferSize (char* p_pInputBufferString);
void EncodeByteTriple (char* p_pInputBuffer, unsigned int InputCharacters, char* p_pOutputBuffer);
unsigned int DecodeByteQuartet (char* p_pInputBuffer, char* p_pOutputBuffer);
public:
CBase64 ();
unsigned int CreateMatchingEncodingBuffer (unsigned int p_InputByteCount, char** p_ppEncodingBuffer);
unsigned int CreateMatchingDecodingBuffer (char* p_pInputBufferString, char** p_ppDecodingBuffer);
void EncodeBuffer (char* p_pInputBuffer, unsigned int p_InputBufferLength, char*p_pOutputBufferString);
unsigned int DecodeBuffer (char* p_pInputBufferString, char* p_pOutputBuffer);
unsigned int EncodeFile (LPTSTR p_pSourceFileName, LPTSTR p_pEncodedFileName);
unsigned int DecodeFile (LPTSTR p_pSourceFileName, LPTSTR p_pDecodedFileName);
// by wsq
unsigned int EncodeBufftoFile (char* p_pInputBuffer, unsigned int p_InputBufferLength, LPTSTR p_pEncodedFileName);
unsigned int DecodeBufftoFile (char* p_pInputBufferString, LPTSTR p_pDecodedFileName);
// by wsq
unsigned int EncodeFiletoBuff (LPTSTR p_pSourceFileName, char* p_pOutputBuffer);
unsigned int DecodeFiletoBuff (LPTSTR p_pSourceFileName, char* p_pOutputBuffer);
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -