1、base64编码的作用:

base64编码的东西,任何人都可以解码,所以称之为加密和解密是不合适的,其作用是:便于数据再网络上的传输。举个简单的例子,你使用SMTP协议 (Simple Mail Transfer Protocol 简单邮件传输协议)来发送邮件,因为这个协议是基于文本的协议,所以,如果邮件中包含一幅图片,我们知道图片的存储格式是二进制数据(binary data)而非文本格式,我们必须将二进制的数据编码成文本格式,这时候Base 64 Encoding就派上用场了。

2、C++实现的base64编码类及其使用方法:

1)、类:

#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string>
using namespace std;namespace {static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I', 'J', 'K', 'L', 'M', 'N', 'O', 'P','Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X','Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f','g', 'h', 'i', 'j', 'k', 'l', 'm', 'n','o', 'p', 'q', 'r', 's', 't', 'u', 'v','w', 'x', 'y', 'z', '0', '1', '2', '3','4', '5', '6', '7', '8', '9', '+', '/' };
static char *decoding_table = NULL;
static int mod_table[] = { 0, 2, 1 };void build_decoding_table()
{decoding_table = (char*)malloc(256);for (int i = 0; i < 64; i++)decoding_table[(unsigned char)encoding_table[i]] = i;
}
char* base64_encode(const unsigned char *data, size_t input_length, size_t *output_length){*output_length = 4 * ((input_length + 2) / 3);char *encoded_data = (char*)malloc(*output_length + 1);if (encoded_data == NULL) return NULL;memset(encoded_data, 0, *output_length + 1);for (int i = 0, j = 0; i < input_length;) {uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];}for (int i = 0; i < mod_table[input_length % 3]; i++)encoded_data[*output_length - 1 - i] = '=';return encoded_data;}
unsigned char* base64_decode(const char *data, size_t input_length, size_t *output_length)
{if (decoding_table == NULL) build_decoding_table();if (input_length % 4 != 0) return NULL;*output_length = input_length / 4 * 3;if (data[input_length - 1] == '=') (*output_length)--;if (data[input_length - 2] == '=') (*output_length)--;unsigned char *decoded_data = (unsigned char*)malloc(*output_length);if (decoded_data == NULL) return NULL;memset(decoded_data, 0, *output_length);for (int i = 0, j = 0; i < input_length;) {uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];uint32_t triple = (sextet_a << 3 * 6)+ (sextet_b << 2 * 6)+ (sextet_c << 1 * 6)+ (sextet_d << 0 * 6);if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;}return decoded_data;
}
void base64_cleanup()
{free(decoding_table);
}
} // namespace

2)、使用注意:

特别要注意的是:在编码前,我们应该将编码内容转换成通用的UTF8格式,解码出来的内容也要转换成自己的编码格式来显示,比如:unicode/ansi等格式。否则的话,对于中文内容,如果和其他语言进行中文内容的传输操作,比如java,它解码出来的内容会是乱码。

3)、编码转换函数,由于我本地是ANSI编码格式,所以下面给出ANSI与UTF8编码格式的相互转换函数,如果你本地使用的是UNICODE编码格式,就要用到UNICODE与UTF8之间的相互转换:

std::string ANSItoUTF8(const char * ansi)
{int len = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);wchar_t* wstr = new wchar_t[len+1];memset(wstr, 0, len+1);MultiByteToWideChar(CP_ACP, 0, ansi, -1, wstr, len);len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);char* str = new char[len+1];memset(str, 0, len+1);WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);if(wstr) delete[] wstr;std::string ret = str;if(str) delete[] str;return ret;
}std::string UTF8toANSI(const char* utf8)
{int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);wchar_t* wstr = new wchar_t[len+1];memset(wstr, 0, len+1);MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);char* str = new char[len+1];memset(str, 0, len+1);WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);if(wstr) delete[] wstr;std::string ret = str;if(str) delete[] str;return ret;
}

4)、实例:

// base64Test.cpp : 定义控制台应用程序的入口点?
//#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string>
using namespace std;namespace {static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I', 'J', 'K', 'L', 'M', 'N', 'O', 'P','Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X','Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f','g', 'h', 'i', 'j', 'k', 'l', 'm', 'n','o', 'p', 'q', 'r', 's', 't', 'u', 'v','w', 'x', 'y', 'z', '0', '1', '2', '3','4', '5', '6', '7', '8', '9', '+', '/' };
static char *decoding_table = NULL;
static int mod_table[] = { 0, 2, 1 };void build_decoding_table()
{decoding_table = (char*)malloc(256);for (int i = 0; i < 64; i++)decoding_table[(unsigned char)encoding_table[i]] = i;
}
char* base64_encode(const unsigned char *data, size_t input_length, size_t *output_length)
{*output_length = 4 * ((input_length + 2) / 3);char *encoded_data = (char*)malloc(*output_length);if (encoded_data == NULL) return NULL;for (int i = 0, j = 0; i < input_length;) {uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];}for (int i = 0; i < mod_table[input_length % 3]; i++)encoded_data[*output_length - 1 - i] = '=';return encoded_data;
}
unsigned char* base64_decode(const char *data, size_t input_length, size_t *output_length)
{if (decoding_table == NULL) build_decoding_table();if (input_length % 4 != 0) return NULL;*output_length = input_length / 4 * 3;if (data[input_length - 1] == '=') (*output_length)--;if (data[input_length - 2] == '=') (*output_length)--;unsigned char *decoded_data = (unsigned char*)malloc(*output_length);if (decoded_data == NULL) return NULL;for (int i = 0, j = 0; i < input_length;) {uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];uint32_t triple = (sextet_a << 3 * 6)+ (sextet_b << 2 * 6)+ (sextet_c << 1 * 6)+ (sextet_d << 0 * 6);if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;}return decoded_data;
}
void base64_cleanup()
{free(decoding_table);
}
} // namespacestd::string ANSItoUTF8(const char * ansi)
{int len = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);wchar_t* wstr = new wchar_t[len+1];memset(wstr, 0, len+1);MultiByteToWideChar(CP_ACP, 0, ansi, -1, wstr, len);len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);char* str = new char[len+1];memset(str, 0, len+1);WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);if(wstr) delete[] wstr;std::string ret = str;if(str) delete[] str;return ret;
}std::string UTF8toANSI(const char* utf8)
{int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);wchar_t* wstr = new wchar_t[len+1];memset(wstr, 0, len+1);MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);char* str = new char[len+1];memset(str, 0, len+1);WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);if(wstr) delete[] wstr;std::string ret = str;if(str) delete[] str;return ret;
}int _tmain(int argc, _TCHAR* argv[])
{std::string strSrc = "我热爱C++语言";string strUtf8 = ANSItoUTF8(strSrc.c_str());fprintf(stdout, "str: %s\n", strSrc.c_str());size_t output_length_encode = 0;char* strEncode = base64_encode((const unsigned char*)strUtf8.c_str(), strUtf8.length(), &output_length_encode);//打印编码结果std::string tmpEncode(strEncode, output_length_encode);fprintf(stdout, "encode result: %s\n", tmpEncode.c_str());size_t output_length_decode = 0;unsigned char* strDecode= base64_decode(strEncode, output_length_encode, &output_length_decode);std::string tmpDecode((char*)strDecode, output_length_decode);//打印解密结果string strDecodeUtf8 = UTF8toANSI(tmpDecode.c_str());fprintf(stdout, "str decode2: %s\n", strDecodeUtf8.c_str());free(strEncode);free(strDecode);base64_cleanup();system("pause");return 0;
}

结果:

与在线的base64编码对照结果:

C++ 使用base64进行编码和解码相关推荐

  1. php base64解码,PHP Base64 中英文编码 JavaScript 解码

    最新PHP Base64 中英文编码 JavaScript 解码 以下是三零网为大家整理的最新PHP Base64 中英文编码 JavaScript 解码的文章,希望大家能够喜欢! function ...

  2. javascript中的Base64.UTF8编码与解码详解

    javascript中的Base64.UTF8编码与解码详解 本文给大家介绍的是javascript中的Base64.UTF8编码与解码的函数源码分享以及使用范例,十分实用,推荐给小伙伴们,希望大家能 ...

  3. 彻底弄懂base64的编码与解码原理

    作者介绍 背景 base64的编码原理网上讲解较多,但解码原理讲解较少,并且没有对其中的内部实现原理进行剖析.想要彻底了解base64的编码与解码原理,请耐心看完此文,你一定会有所收获. 涉及算法与逻 ...

  4. img图片的预览和下载(iframe基本使用),图片转file格式,file文件转base64格式,base64的编码和解码

    目录 一.根据后端接口返回的URL下载和预览图片 1.调用下载图片函数实现下载图片 2.弹出另一个页面,实现预览图片. 3.不弹出另一个页面,实现下载/预览图片(iframe方式) 4.动态创建ifr ...

  5. atob()和btoa() 进行base64的编码和解码

    编码: 解码:

  6. btoa java_javascript atob()函数和 btoa()函数-Base64的编码与解码-Go语言中文社区

    在 JavaScript 中,有两个函数被分别用来处理解码和编码 base64 字符串: atob() //ASCII to Base64 btoa() //Base64 to ASCII atob( ...

  7. javascript atob()函数和 btoa()函数-Base64的编码与解码

    在 JavaScript 中,有两个函数被分别用来处理解码和编码 base64 字符串: atob() //ASCII to Base64 btoa() //Base64 to ASCII atob( ...

  8. js中使用btoa和atob进行Base64的编码和解码

    window.atob(string):解码 用来解码一个已经被base-64编码过的字符串.(参数string: 必需是一个通过 btoa() 方法编码的字符串) window.btoa(strin ...

  9. Base64的编码和解码

    为什么要使用Base64? 在设计这个编码的时候,我想设计人员最主要考虑了3个问题: 1.是否加密? 2.加密算法复杂程度和效率 3.如何处理传输? 加密是肯定的,但是加密的目的不是让用户发送非常安全 ...

  10. C# Base64图片编码和解码

    Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法.可查看RFC2045-RFC2049,上面有MIME的详细规范. Ba ...

最新文章

  1. jsp中九大内置对象
  2. linux系统目录树/内核源码目录树
  3. docker实战部署Javaweb项目
  4. mybatis注解配置出现returned more than one row, where no more than one was expected
  5. jni返回byte[]
  6. 深度学习100例-卷积神经网络(CNN)3D医疗影像识别 | 第23天
  7. 神策学堂“训练营+特训营”,种子学员招募中,来一起出圈呀!
  8. 神策用户画像 2 大功能:千人千面,一人千面,一次给你
  9. ARIA and the value of challenge-led innovation
  10. java 数字 下划线_为什么要在Java SE 7的数字中使用下划线-在数字文字中使用下划线...
  11. C#枚举类型的常用操作总结
  12. pico park无法连接至远程服务器,pico park怎么联机玩?pico park怎么邀请朋友一起玩?[多图]...
  13. hdu3507 print article
  14. 未解决:运行EtherCalc出错:Error: Cannot find module 'zappajs'
  15. 如何编写银行转账的测试用例,可以来看这里.....
  16. 互联网最强的下载工具:IDM
  17. 新会计准则 计算机管理系统,用友ERP供应链管理系统实验教程(新会计准则版)pdf...
  18. Java进阶之路对标阿里P6(8)——分布式理论及框架设计Netty
  19. android设备刷机精灵,使用刷机精灵给安卓手机刷机教程
  20. Stacked Hourglass Networks 理解

热门文章

  1. 华为机试HJ97:记负均正
  2. 通过程序(C# C/S)调用Sato Label Gallery标签模板 打印
  3. python中列表嵌套字典/列表排序,字典排序,列表排序。
  4. 学习开源播放器代码解析之参数设置
  5. 数据分析--分类模型
  6. 2017.04.01号最新cpu天梯图 包含7代cpu
  7. 词云修改样式——python
  8. MR 切片机制 MR全流程
  9. 安科瑞餐饮环保油烟在线监测大平台
  10. 选择拉绳传感器需要符合哪几个条件?