#ifndef ___BASE64_H___
#define ___BASE64_H___ #include <string> using namespace std; class CBase64
{
public:
CBase64();
~CBase64(); /*********************************************************
* 函数说明:将输入数据进行base64编码
* 参数说明:[in]pIn 需要进行编码的数据
[in]uInLen 输入参数的字节数
[out]strOut 输出的进行base64编码之后的字符串
* 返回值 :true处理成功,false失败
**********************************************************/
bool static Encode(const unsigned char *pIn, unsigned long uInLen, string& strOut); /*********************************************************
* 函数说明:将输入数据进行base64编码
* 参数说明:[in]pIn 需要进行编码的数据
[in]uInLen 输入参数的字节数
[out]pOut 输出的进行base64编码之后的字符串
[out]uOutLen 输出的进行base64编码之后的字符串长度
* 返回值 :true处理成功,false失败
**********************************************************/
bool static Encode(const unsigned char *pIn, unsigned long uInLen, unsigned char *pOut, unsigned long *uOutLen); /*********************************************************
* 函数说明:将输入数据进行base64解码
* 参数说明:[in]strIn 需要进行解码的数据
[out]pOut 输出解码之后的节数数据
[out]uOutLen 输出的解码之后的字节数长度
* 返回值 :true处理成功,false失败
**********************************************************/
bool static Decode(const string& strIn, unsigned char *pOut, unsigned long *uOutLen) ; /*********************************************************
* 函数说明:将输入数据进行base64解码
* 参数说明:[in]strIn 需要进行解码的数据
[out]pOut 输出解码之后的节数数据
[out]uOutLen 输出的解码之后的字节数长度
* 返回值 :true处理成功,false失败
**********************************************************/
bool static Decode(const unsigned char *pIn, unsigned long uInLen, unsigned char *pOut, unsigned long *uOutLen) ;
}; #endif // ___BASE64_H___
#include "stdafx.h"

/*
*
*Base64?
*
*
*
*/ #include "Base64.h" static const char *g_pCodes =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; static const unsigned char g_pMap[] =
{
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , ,
}; CBase64::CBase64()
{
} CBase64::~CBase64()
{
} bool CBase64::Encode(const unsigned char *pIn, unsigned long uInLen, unsigned char *pOut, unsigned long *uOutLen)
{
unsigned long i, len2, leven;
unsigned char *p; if(pOut == NULL || *uOutLen == )
return false; //ASSERT((pIn != NULL) && (uInLen != 0) && (pOut != NULL) && (uOutLen != NULL)); len2 = ((uInLen + ) / ) << ;
if((*uOutLen) < (len2 + )) return false; p = pOut;
leven = * (uInLen / );
for(i = ; i < leven; i += )
{
*p++ = g_pCodes[pIn[] >> ];
*p++ = g_pCodes[((pIn[] & ) << ) + (pIn[] >> )];
*p++ = g_pCodes[((pIn[] & 0xf) << ) + (pIn[] >> )];
*p++ = g_pCodes[pIn[] & 0x3f];
pIn += ;
} if (i < uInLen)
{
unsigned char a = pIn[];
unsigned char b = ((i + ) < uInLen) ? pIn[] : ;
unsigned char c = ; *p++ = g_pCodes[a >> ];
*p++ = g_pCodes[((a & ) << ) + (b >> )];
*p++ = ((i + ) < uInLen) ? g_pCodes[((b & 0xf) << ) + (c >> )] : '=';
*p++ = '=';
} *p = ; // Append NULL byte
*uOutLen = p - pOut;
return true;
} bool CBase64::Encode(const unsigned char *pIn, unsigned long uInLen, string& strOut)
{
unsigned long i, len2, leven;
strOut = ""; //ASSERT((pIn != NULL) && (uInLen != 0) && (pOut != NULL) && (uOutLen != NULL)); len2 = ((uInLen + ) / ) << ;
//if((*uOutLen) < (len2 + 1)) return false; //p = pOut;
leven = * (uInLen / );
for(i = ; i < leven; i += )
{
strOut += g_pCodes[pIn[] >> ];
strOut += g_pCodes[((pIn[] & ) << ) + (pIn[] >> )];
strOut += g_pCodes[((pIn[] & 0xf) << ) + (pIn[] >> )];
strOut += g_pCodes[pIn[] & 0x3f];
pIn += ;
} if (i < uInLen)
{
unsigned char a = pIn[];
unsigned char b = ((i + ) < uInLen) ? pIn[] : ;
unsigned char c = ; strOut += g_pCodes[a >> ];
strOut += g_pCodes[((a & ) << ) + (b >> )];
strOut += ((i + ) < uInLen) ? g_pCodes[((b & 0xf) << ) + (c >> )] : '=';
strOut += '=';
} //*p = 0; // Append NULL byte
//*uOutLen = p - pOut;
return true;
} bool CBase64::Decode(const string& strIn, unsigned char *pOut, unsigned long *uOutLen)
{
unsigned long t, x, y, z;
unsigned char c;
unsigned long g = ; //ASSERT((pIn != NULL) && (uInLen != 0) && (pOut != NULL) && (uOutLen != NULL)); for(x = y = z = t = ; x < strIn.length(); x++)
{
c = g_pMap[strIn[x]];
if(c == ) continue;
if(c == ) { c = ; g--; } t = (t << ) | c; if(++y == )
{
if((z + g) > *uOutLen) { return false; } // Buffer overflow
pOut[z++] = (unsigned char)((t>>)&);
if(g > ) pOut[z++] = (unsigned char)((t>>)&);
if(g > ) pOut[z++] = (unsigned char)(t&);
y = t = ;
}
} *uOutLen = z;
return true;
}

最新文章

  1. C、C++: 引用、指针、实例、内存模型、namespace
  2. web开发以及分布式开发C/S B/S系统结构
  3. hadoop的live node为0
  4. 使用bat快速创建cocos2d-x模板
  5. (转)基于即时通信和LBS技术的位置感知服务(一):提出问题及解决方案
  6. 使用XLinq.XElement读取带Namespace(命名空间)的XML
  7. Node.js v0.10.31API手冊-事件
  8. 基于Linux平台病毒BlackHole病毒的决心
  9. leetcode day5
  10. 【SpringMVC】XML配置说明
  11. unity中object 对象之间用c# delegate方式进行通信
  12. windows 上用 docker 部署aspnetcore 2.0
  13. Java 内部类的简单介绍
  14. Reveal 破解及使用
  15. Jmeter使用笔记之组件的作用域
  16. ELK 的插件安装(head)
  17. HDU 6205(尺取法)2017 ACM/ICPC Asia Regional Shenyang Online
  18. eclipse A Java Runtime Environment(JRE)
  19. MySQL索引优化经验总结
  20. python 字符串的格式化

热门文章

  1. 使用 Azure PowerShell 监视和更新 Windows 虚拟机
  2. python之demo1----改编自turtle.py文件中的demo
  3. SQL删除多列语句
  4. 数据库导入.bacpac 文件创建新实例
  5. easyui中日期格式化
  6. Java中几种常用数据类型之间转换的方法
  7. MySQL一个延迟案例
  8. MySQL二进制日志文件Binlog的三种格式以及对应的主从复制中三种技术
  9. MYSQL导入csv类型的数据出现The MySQL server is running with the --secure-file-priv option
  10. Beta阶段第二次冲刺