//https://en.wikipedia.org/wiki/Base64

std::string base64Encode(const std::vector<char>& byteData);

std::vector<char> base64Decode(std::string & const inputString);

std::string Cbase64Dlg::base64Encode(const std::vector<char>& byteData)
{
   const std::string codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   std::string base64String;
   int b;
   for (size_t i = 0; i < byteData.size(); i = i+3)
   {
      b = (byteData[i] & 0xFC) >> 2;
      base64String.push_back(codes[b]);
      b = (byteData[i] & 0x03) << 4;
      if (i + 1 < byteData.size())
      {
         b |= (byteData[i + 1] & 0xF0) >> 4;
         base64String.push_back(codes[b]);
         b = (byteData[i + 1] & 0x0F) << 2;
         if (i+2 < byteData.size())
         {
            b |= (byteData[i + 2] & 0xC0) >> 6;
            base64String.push_back(codes[b]);
            b = byteData[i + 2] & 0x3F;
            base64String.push_back(codes[b]);
         }
         else
         {
            base64String.push_back(codes[b]);
            base64String.append("=");
         }
      }
      else
      {
         base64String.push_back(codes[b]);
         base64String.append("==");
      }
   }
   
   return base64String;
}

std::vector<char> Cbase64Dlg::base64Decode(std::string & const inputString)
{   
   static std::string codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   std::vector<char> decoded;

if (inputString.length() % 4 != 0)    
   {
      return std::vector<char>();
   }

//The ratio of output bytes to input bytes is 4:3
   int outLen = (inputString.length() * 3 / 4);

size_t pos = inputString.find_first_of('=');
   if (pos != std::string::npos)
   {
      decoded.resize(outLen - (inputString.length() - pos));
   }
   else
   {
      decoded.resize(outLen);
   }

int j = 0;
   int b[4] = {};

const char* p = inputString.c_str();

while(p && *p && j < outLen)
   {
      bool valid = false;
      for (int i=0; p && i < 4; ++i)
      {
         size_t pos = codes.find_first_of(*p++);
         if ( pos != std::string::npos)
         {
            b[i] = pos;
         }
      }

if (j < outLen)
      {
         decoded[j++] = (byte) ((b[0] << 2) | (b[1] >> 4));;
         if (j < outLen && b[2] < 64)      
         {
            decoded[j++] = (byte) ((b[1] << 4) | (b[2] >> 2));

if (j < outLen && b[3] < 64)  
            {
               decoded[j++] = (byte) ((b[2] << 6) | b[3]);
            }
         }
      }
   }

return decoded;
}
void Cbase64Dlg::OnBnClickedButton1()
{
   char myints[] = "ABC&&&&&&&&&&";
   std::vector<char> byte (myints, myints + sizeof(myints) / sizeof(char) );
   std::string value = base64Encode(byte);
   std::cout << value << std::endl;
   std::vector<char>decode = base64Decode(value);
}

base64 encoding的更多相关文章

  1. Control character in cookie value, consider BASE64 encoding your value , java操作cookie遇到中文会报错的解决方案

    项目当中用到cookie保存中文,但是会报如下错误: Control character in cookie value, consider BASE64 encoding your value 大概 ...

  2. Control character in cookie value, consider BASE64 encoding your value-Cookie保存中文出错[转]

    项目当中用到cookie保存中文,但是会报如下错误: Control character in cookie value, consider BASE64 encoding your value 大概 ...

  3. Node.js Base64 Encoding和Decoding

    如何在Node.js中encode一个字符串呢?是否也像在PHP中使用base64_encode()一样简单? 在Node.js中有许多encoding字符串的方法,而不用像在JavaScript中那 ...

  4. Base64 Encoding / Decoding in Node.js

    Posted on April 20th, 2012 under Node.js Tags: ASCII, Buffer, Encoding, node.js, UTF So how do you e ...

  5. 使用Cookie报错Control character in cookie value, consider BASE64 encoding your value

    参考资料: http://www.blogjava.net/persister/archive/2009/10/02/297103.html http://blog.csdn.net/xiaozhen ...

  6. Control character in cookie value, consider BASE64 encoding your value

    这是因为你给Cookie设置了中文的value,比如Cookie c = new Cookie("user", "张三");

  7. (iOS)Base64加密和DES加密、以及JAVA和iOS中DES加密统一性问题

    我们在项目中为了安全方面的考虑,通常情况下会选择一种加密方式对需要安全性的文本进行加密,而Base64加密和DES64加密是常用的加密算法.我记得我在前一个项目中使用的就是这两种加密算法的结合:Bas ...

  8. [c] base64

    / * Program: * base64 encode & decode * Author: * brant-ruan * Date: * 2016-02-29 * Usage: * Enc ...

  9. Base64编码格式详解

    什么是Base64? 按照RFC2045的定义,Base64被定义为:Base64内容传送编码被设计用来把任意序列的8位字节描述为一种不易被人直接识别的形式.(The Base64 Content-T ...

随机推荐

  1. 求一个全排列函数: 如p([1,2,3])输出:[123],[132],[213],[231],[312],[321]. 求一个组合函数 如p([1,2,3])输出:[1],[2],[3],[1,2],[2,3],[1,3],[1,2,3]

    深度搜索的代码: #include<stdio.h> #include<string.h> ; int n; int a[Max]; bool b[Max]; void Dfs ...

  2. SQLHelper.cs的经典代码-存储过程

    using System; using System.Collections.Generic; using System.Text; using System.Collections; using S ...

  3. Hive常用的SQL命令操作

    Hive提供了很多的函数,可以在命令行下show functions罗列所有的函数,你会发现这些函数名与mysql的很相近,绝大多数相同的,可通过describe function functionN ...

  4. ios开发--第三方整理

    一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https://github. ...

  5. PHP组合模式、策略模式

    一.问题 模拟不同课程有不同的收费方式,并且能灵活改变(新增或删减),如讲座可以固定收费也可改为按时收费,研讨会也是. 二.模式简介及关键点 1.在父类代码中使用条件语句是一种退倒,可以用多态来代替条 ...

  6. JLINK固件丢失或升级固件后提示Clone的解决办法

    J-LINK V8固件烧录指导 J-LINK 是使用过程中,如果内部固件意外损坏或丢失,请参考下面操作步骤说明,重新烧录JLINK固件. 安装固件烧录软件 请ATMEL官方网址下载AT91-ISP下载 ...

  7. Knight's Trip---hdu3766(马走日求最小走的步数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3766 给你一个x ,y 求出从(0,0)位置到达需要的最小步数每次只能走日型: 下图为暴力bfs得到的 ...

  8. DB2操作流程

    DB2如何创建表空间 如何创建数据库 如何创建缓冲池标签: db2数据库system脚本linuxwindows2012-06-13 19:16 8411人阅读 评论(0) 收藏 举报 版权声明:本文 ...

  9. AngularJs+bootstrap搭载前台框架——准备工作

    1.关于什么是AngularJs以及什么是bootstrap我就不多说了,简单说下,AngularJs是一个比较强大前台MVC框架,bootstrap是Twitter推出的一个用于前端开发的开源工具包 ...

  10. hive报错 Another instance of Derby may have already booted the database

    刚装好hive后,启动之后showtables;等正常,退出之后再进入,就发现会报错 Caused by: ERROR XSDB6: Another instance ofDerbymay have ...