热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

base64编码c++版本

C++版本的base64编码,使用vector和sring做为输入输出,原文出自http:www.adp-gmbh.chcppcommonbase64.html,稍微做了一些改动:

C++版本的base64编码,使用vector和sring做为输入输出,原文出自http://www.adp-gmbh.ch/cpp/common/base64.html,稍微做了一些改动:

#ifndef BASE64_H
#define BASE64_H
#include
#include
std::string base64_encode(unsigned char const* , unsigned int len);
std::vector base64_decode(std::string const& s);
#endif

/*
base64.cpp and base64.h
Copyright (C) 2004-2008 Ren茅 Nyffenegger
This source code is provided ‘as-is‘, without any express or implied
warranty. In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this source code must not be misrepresented; you must not
claim that you wrote the original source code. If you use this source code
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original source code.
3. This notice may not be removed or altered from any source distribution.
Ren茅 Nyffenegger rene.nyffenegger@adp-gmbh.ch
dotphoenix(dotphoenix@qq.com) modified at 20140220:
change the decode out put to std::vector
*/
#include "base64.h"
#include
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == ‘+‘) || (c == ‘/‘));
}
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len--) {
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) <<4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) <<2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if (i)
{
for(j = i; j <3; j++)
char_array_3[j] = ‘\0‘;
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) <<4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) <<2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j ret += base64_chars[char_array_4[j]];
while((i++ <3))
ret += ‘=‘;
}
return ret;
}
std::vector base64_decode(std::string const& encoded_string) {
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::vector ret;
while (in_len-- && ( encoded_string[in_] != ‘=‘) && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] <<2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) <<4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) <<6) + char_array_4[3];
for (i = 0; (i <3); i++)
ret.push_back(char_array_3[i]) ;
i = 0;
}
}
if (i) {
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] <<2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) <<4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) <<6) + char_array_4[3];
for (j = 0; (j {
ret.push_back(char_array_3[j]);
}
}
return ret;
}


#include
void base64_tester()
{
#define BINAY_LENGTH 17
unsigned char binary_data[BINAY_LENGTH] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x11, 0x3A, 0x4B, 0x5C, 0x6D, 0x7E, 0x8F, 0xA0, 0xFE, 0xFF};
std::string base64 = base64_encode(binary_data, BINAY_LENGTH);
std::vector binary = base64_decode(base64);
assert(binary.size() == BINAY_LENGTH);
assert(binary[0] == binary_data[0]);
assert(binary[BINAY_LENGTH - 1] == binary_data[BINAY_LENGTH - 1]);
}



推荐阅读
  • 内容java基础巩固笔记-实现AOP功能的封装与配置的小框架设计(目录):XXXjava.util.ArrayList中代码Advice接口MyAdvice类BeanFactory ... [详细]
  • 这是一道典型的强连通的题目。 所谓强连通,就是对于一个有向图,若一个集合内任意2点都能过互相达,于是这个几何就是一个强连通分量。 对于任意图,都可以分解人多个不相交的强连通集合。  ... [详细]
  • 恢复内容开始作用域分别为:当前对象、方法内部、类;局部变量:在方法体中定义的变量,局部变量只在定义它的方法中有效。成员变量:在整个类中都有效(全局变量是C语言中的叫法,Java中没 ... [详细]
  • 答题:消息队列的核心功能就是:解耦合,异步,流量削峰解耦:接口调用发送,那如果E系统也要这个数据呢?那如果C系统现在不需要了呢?现在A系统又要发送第二种数据了呢?A系统负责人濒临崩 ... [详细]
  • 操作系统原理之I/O设备管理(第六章上半部分下)
    中断处理程序的作?:IO中断处理程序的作?是将发出IO请求?被 ... [详细]
  • .NET Web应用程序安装包的制作经历:Sql数据库安装的3种方式
    一次难得的安装包制作经历,因为之前从没有制作过安装包,那就免不了遇到问题,在摸索和学习中获得了不少宝贵经验,在这里我将用图文并茂的形式详细描述一下流程及主要难点问题的解决方法,希望 ... [详细]
  • J ... [详细]
  • httprunner3.X相比httprunner2.X系统中会新增4个命令:httprunner:核心命令hrun:httprunner的缩写,功能与httprunner完全相同 ... [详细]
  • 2.2 与球体相交几何解
    现在,一个简单的球体相交例子已经被概述。这里有一些关于计算效率的概念。一个普遍的看法是,应该尽可能地避免使用平方根函数。检查计时:sqrt()耗时通常是乘法的15~30倍。类似的, ... [详细]
  • 2017年5月24日星期三--出埃及记Exodus26:12Asfortheadditionallengthofthetentcurtains,thehalfcurtainthat ... [详细]
  • 本次实验目的,使用Pacemaker实现DRBD存储及应用高可用?实验环境:系统版本:CentOSrelease6.5(Final)_x64node1:ip:192.168.0.2 ... [详细]
  • ylbtechLanguageSamplesEvents(事件)
    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Events(事件)1.A,示例(Sample)返回顶部“事件”示例 ... [详细]
  • 程序运行时间要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间。这个时间单 ... [详细]
  • 一、昨天做的因为今天是第一天,所以昨天只是开了个会,分配了一些任务,我和小组的另一成员zyp做的是开机启动的代码实现。二、遇到的问题今天做的是开机启动的功能。在开机启动的代码上,我 ... [详细]
  • 6年前的一个U盘记得还是参加某会议送的,当时做了量产多做了一个光盘区,现在用不着了想还原成普通U盘忘了方法。可见笔记的重要性Chipgenius看了主控 PS2251-50芯片是海 ... [详细]
author-avatar
lmaster
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有