base 64 编解码


1. base64的编码都是按字符串长度,以每3个8bit的字符为一组,


2. 然后针对每组。首先获取每一个字符的ASCII编码。


3. 然后将ASCII编码转换成8bit的二进制,得到一组3*8=24bit的字节


4. 然后再将这24bit划分为4个6bit的字节,并在每一个6bit的字节前面都填两个高位0。得到4个8bit的字节


5. 然后将这4个8bit的字节转换成10进制。对比Base64编码表 (下表)。得到相应编码后的字符。


watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHVoZWF6eA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">


实现:

//////////////////////////////////////////////////////////////////////
//
// base64 Encoding/Decoding:
// Encoding: String2Base64: unsigned char * to base64;
// Decoding: Base642TString: base64 to unsigned char * .
//
// xuhh
// Dec 11, 2014
//
////////////////////////////////////////////////////////////////////// #if !defined(_MIME_CODING_H)
#define _MIME_CODING_H #if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000 #include <utility>
#include <string>
#include <list>
using namespace std; #if !defined(ASSERT)
#if defined(_DEBUG)
#include <assert.h>
#define ASSERT(exp) assert(exp)
#else
#define ASSERT(exp) ((void)0)
#endif
#endif #if defined(_DEBUG) && !defined(DEBUG_NEW)
#define DEBUG_NEW new
#endif // maximum length of an encoded line (RFC 2045)
#define MAX_MIME_LINE_LEN 76
#define MAX_ENCODEDWORD_LEN 75 //////////////////////////////////////////////////////////////////////
// string to base64 extern unsigned char * String2Base64(const char* str); //////////////////////////////////////////////////////////////////////
// base64 to string extern unsigned char * Base642TString(const char* str); //////////////////////////////////////////////////////////////////////
// CMimeEnvironment - global environment to manage encoding/decoding class CMimeCodeBase; #define DECLARE_MIMECODER(class_name) \
public: static CMimeCodeBase* CreateObject() { return new class_name; } //////////////////////////////////////////////////////////////////////
// CMimeCodeBase class CMimeCodeBase
{
public:
CMimeCodeBase() :
m_pbInput(NULL),
m_nInputSize(0),
m_bIsEncoding(false) {} public:
void SetInput(const char* pbInput, int nInputSize, bool bEncoding)
{
m_pbInput = (const unsigned char*) pbInput;
m_nInputSize = nInputSize;
m_bIsEncoding = bEncoding;
}
int GetOutputLength() const
{
return m_bIsEncoding ? GetEncodeLength() : GetDecodeLength();
}
int GetOutput(unsigned char* pbOutput, int nMaxSize)
{
return m_bIsEncoding ? Encode(pbOutput, nMaxSize) : Decode(pbOutput, nMaxSize);
} protected:
// overrides
virtual int GetEncodeLength() const { return m_nInputSize; }
virtual int GetDecodeLength() const { return m_nInputSize; }
virtual int Encode(unsigned char* pbOutput, int nMaxSize) const
{
int nSize = min(nMaxSize, m_nInputSize);
::memcpy(pbOutput, m_pbInput, nSize);
return nSize;
}
virtual int Decode(unsigned char* pbOutput, int nMaxSize)
{
return CMimeCodeBase::Encode(pbOutput, nMaxSize);
} protected:
const unsigned char* m_pbInput;
int m_nInputSize;
bool m_bIsEncoding;
}; //////////////////////////////////////////////////////////////////////
// CMimeCodeBase64 - for base64 encoding mechanism class CMimeCodeBase64 : public CMimeCodeBase
{
public:
CMimeCodeBase64() :
m_bAddLineBreak(true) {} public:
DECLARE_MIMECODER(CMimeCodeBase64)
void AddLineBreak(bool bAdd=true) { m_bAddLineBreak = bAdd; } protected:
virtual int GetEncodeLength() const;
virtual int GetDecodeLength() const;
virtual int Encode(unsigned char* pbOutput, int nMaxSize) const;
virtual int Decode(unsigned char* pbOutput, int nMaxSize); private:
bool m_bAddLineBreak; private:
static inline int DecodeBase64Char(unsigned int nCode)
{
if (nCode >= 'A' && nCode <= 'Z')
return nCode - 'A';
if (nCode >= 'a' && nCode <= 'z')
return nCode - 'a' + 26;
if (nCode >= '0' && nCode <= '9')
return nCode - '0' + 52;
if (nCode == '+')
return 62;
if (nCode == '/')
return 63;
return 64;
}
}; #endif // !defined(_MIME_CODING_H)


实现文件:



//////////////////////////////////////////////////////////////////////
//
// base64 Encoding/Decoding:
// Encoding: String2Base64: unsigned char * to base64;
// Decoding: Base642TString: base64 to unsigned char * .
//
// xuhh
// Dec 11, 2014
//
//////////////////////////////////////////////////////////////////////
#include "MimeCode.h" #ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif //////////////////////////////////////////////////////////////////////////
// string to base64
unsigned char* String2Base64(const char * szStr)
{
CMimeCodeBase64 base64;
base64.SetInput(szStr, strlen(szStr), true);
int nLen = base64.GetOutputLength()+1;
unsigned char* pOutput = new unsigned char[nLen];
nLen = base64.GetOutput(pOutput, nLen);
pOutput[nLen] = 0;
return pOutput;
} //////////////////////////////////////////////////////////////////////////
// base64 to string unsigned char * Base642TString(const char* str)
{
CMimeCodeBase64 base64;
base64.SetInput(str, strlen(str), false);
int len = base64.GetOutputLength()+1;
unsigned char* pOutput = new unsigned char[len];
len = base64.GetOutput(pOutput, len);
pOutput[len] = 0;
return pOutput;
} //////////////////////////////////////////////////////////////////////
// CMimeCodeBase64 int CMimeCodeBase64::GetEncodeLength() const
{
int nLength = (m_nInputSize + 2) / 3 * 4;
if (m_bAddLineBreak)
nLength += (nLength / MAX_MIME_LINE_LEN + 1) * 2;
return nLength;
} int CMimeCodeBase64::GetDecodeLength() const
{
return m_nInputSize * 3 / 4 + 2;
} int CMimeCodeBase64::Encode(unsigned char* pbOutput, int nMaxSize) const
{
static const char* s_Base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; unsigned char* pbOutStart = pbOutput;
unsigned char* pbOutEnd = pbOutput + nMaxSize;
int nFrom, nLineLen = 0;
unsigned char chHigh4bits = 0; for (nFrom=0; nFrom<m_nInputSize; nFrom++)
{
if (pbOutput >= pbOutEnd)
break; unsigned char ch = m_pbInput[nFrom];
switch (nFrom % 3)
{
case 0:
*pbOutput++ = s_Base64Table[ch >> 2];
chHigh4bits = (ch << 4) & 0x30;
break; case 1:
*pbOutput++ = s_Base64Table[chHigh4bits | (ch >> 4)];
chHigh4bits = (ch << 2) & 0x3c;
break; default:
*pbOutput++ = s_Base64Table[chHigh4bits | (ch >> 6)];
if (pbOutput < pbOutEnd)
{
*pbOutput++ = s_Base64Table[ch & 0x3f];
nLineLen++;
}
} nLineLen++;
if (m_bAddLineBreak && nLineLen >= MAX_MIME_LINE_LEN && pbOutput+2 <= pbOutEnd)
{
*pbOutput++ = '\r';
*pbOutput++ = '\n';
nLineLen = 0;
}
} if (nFrom % 3 != 0 && pbOutput < pbOutEnd) // 不足三位。= 补足
{
*pbOutput++ = s_Base64Table[chHigh4bits];
int nPad = 4 - (nFrom % 3) - 1;
if (pbOutput+nPad <= pbOutEnd)
{
::memset(pbOutput, '=', nPad);
pbOutput += nPad;
}
}
if (m_bAddLineBreak && nLineLen != 0 && pbOutput+2 <= pbOutEnd)
{
*pbOutput++ = '\r';
*pbOutput++ = '\n';
}
return (int)(pbOutput - pbOutStart);
} int CMimeCodeBase64::Decode(unsigned char* pbOutput, int nMaxSize)
{
const unsigned char* pbData = m_pbInput;
const unsigned char* pbEnd = m_pbInput + m_nInputSize;
unsigned char* pbOutStart = pbOutput;
unsigned char* pbOutEnd = pbOutput + nMaxSize; int nFrom = 0;
unsigned char chHighBits = 0; while (pbData < pbEnd)
{
if (pbOutput >= pbOutEnd)
break; unsigned char ch = *pbData++;
if (ch == '\r' || ch == '\n')
continue;
ch = (unsigned char) DecodeBase64Char(ch);
if (ch >= 64) // invalid encoding, or trailing pad '='
break; switch ((nFrom++) % 4)
{
case 0:
chHighBits = ch << 2;
break; case 1:
*pbOutput++ = chHighBits | (ch >> 4);
chHighBits = ch << 4;
break; case 2:
*pbOutput++ = chHighBits | (ch >> 2);
chHighBits = ch << 6;
break; default:
*pbOutput++ = chHighBits | ch;
}
} return (int)(pbOutput - pbOutStart);
}

base 64 编解码器的更多相关文章

  1. C# base 64图片编码解码

    使用WinForm实现了图片base64编码解码的 效果图: 示例base 64编码字符串: /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKD ...

  2. Base 64 编码

    原创地址:http://www.cnblogs.com/jfzhu/p/4020097.html 转载请注明出处 (一)Encoding VS. Encryption 很多人都以为编码(Encodin ...

  3. 浅谈 Data URI 与 BASE 64 编码

    前言(废话):鼓捣 Stylish 的时候发现了这么个奇怪的代码行: Data:image/gif;BASE64,R0lGODlhEAAQAKEAAEKF9NPi/AAAAAAAACH5BAEAAAI ...

  4. Base 64 & decodeURIComponent

    Base 64 & decodeURIComponent js btoa() & atob() let obj = [{"key":"q",&q ...

  5. 关于BASE 24 ,BASE 64原理以及实现程序

    关于BASE 24 ,BASE 64原理以及实现程序 来源 https://wangye.org/blog/archives/5/ 可能很多人听说过Base64编码,很少有人听说过Base24编码,B ...

  6. Base 64 & URL & blob & FileReader & createObjectURL

    Base 64 & URL & blob & FileReader & createObjectURL /** * let blob = item.getAsFile( ...

  7. base 64 & blob & image url

    base 64 & blob & image url base 64 image & e.clipboardData.items[1] https://codepen.io/x ...

  8. 快速掌握 Base 64 | 学 Java 密码系列

    Java 密码系列 - Java 和 JS Base 64 Base 64 不属于密码技术,仅是编码方式.但由于在 Java.JavaScript.区块链等出现的频率较高,故在本系列文章中首先分享 B ...

  9. Base 64 加密、解密

    1.写一个公共类 package com.boyutec.oss.sys.utils; import java.io.UnsupportedEncodingException; import java ...

随机推荐

  1. MySQL字符串函数、日期时间函数

    MySQL字符串函数.日期时间函数 一.常见字符串函数: 1.CHAR_LENGTH  获取长度(字符为单位) 2.FORMAT  格式化 3.INSERT  替换的方式插入 4.INSTR  获取位 ...

  2. Jquery节点遍历

    jquery 节点遍历 <html> <head> <title></title> <script src="Jquery/jquery ...

  3. Android比较字符串是空的(isEmpty)

    通常情况下,我们需要去推断一个字符串变量是否为空,今天,我特意做了一个小测试 StringUtils.java: package com.yx.equipment_collection.utils; ...

  4. POJ题目分类【实在是不知道哪个是原创了】

    原地址:http://blog.csdn.net/liuqiyao_01/article/details/8477801 初期:一.基本算法:     (1)枚举. (poj1753,poj2965) ...

  5. php学习之道:WSDL具体解释(三)

    通过声明方式定义绑定(binding)属性 假设你在服务中採用SOAP binding.你能够使用JAX-WS来指定一定数量的属性binding. 这些属性指定相应你在WSDL中指定的属性.某些设置. ...

  6. python实现了字符串的按位异或和php中的strpad函数

    近期在写自己主动化測试,因为开发加密中用到strpad和字符串的按位异或,而python中没有这种函数和功能,所以必须自己写一套,要不自己主动化測试无法进行,所以就用python实现了一下,因为在写字 ...

  7. Android 通过调用系统,如接口 谷歌语音、百度语音、科大讯飞语音等语音识别方法对话框

    现在app在发展过程中会集成一些语音识别功能,不具有其自己的显影剂一般正在开发的语音识别引擎,所以在大多数情况下,它是选择一个成熟的语音识别引擎SDK集成到他们的app在. 平时,这种整合被分成两个, ...

  8. Linux下精确控制时间的函数

    Linux下精确控制时间的函数 在测试程序接口运行时间的时候,常用time,gettimeofday等函数,但是这些函数在程序执行的时候是耗费时间的,如果仅仅测试时间还行,但是如果程序中用到时间控制类 ...

  9. 详解CMS垃圾回收机制

    原创不易,未经允许,不得转载~~~ 什么是CMS? Concurrent Mark Sweep. 看名字就知道,CMS是一款并发.使用标记-清除算法的gc. CMS是针对老年代进行回收的GC. CMS ...

  10. PHP_保留两位小数而且四舍五入_保留两位小数而且不四舍五入

    php保留两位小数而且四舍五入 $num = 123213.666666; echo sprintf("%.2f", $num); php保留两位小数而且不四舍五入 $num =  ...