//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. list、set、map的特点

    java 集合(list.set.map)的特点 集合相关的类有一大堆,一般也只用到常用的方法增删改查,而且它它们的方法名也基本一样,所以一直都不知道什么时候用什么集合, 今天趁有空特意从网上整理资料 ...

  2. ubuntu下安装与测试mysql

    1.在决定安装mysql之前,要先确定系统是否已经安装mysql. 输入: 1 mysql 结果:说明尚未安装mysql The program 'mysql' is currently notins ...

  3. word2vec——高效word特征提取

    继上次分享了经典统计语言模型,最近公众号中有很多做NLP朋友问到了关于word2vec的相关内容, 本文就在这里整理一下做以分享. 本文分为 概括word2vec 相关工作 模型结构 Count-ba ...

  4. USACO Section 3.2: Feed Ratios

    直接暴力搜 /* ID: yingzho1 LANG: C++ TASK: ratios */ #include <iostream> #include <fstream> # ...

  5. PHP 投票练习

    重点:1.进度条的显示2.操作数据库<form action="chuli.php" method="post"> <?php include ...

  6. 【Tech】android真机测试——小米3

    开始学习android了,自带的虚拟AVD慢的不忍直视,只能拿自己的小米3开刀了.弄了好久,记录如下. 首先,我承认到现在我不知道小米3的驱动到底是怎么安装的,我按照网上的方法自己下载过小米的驱动,但 ...

  7. 无线路由器WDS设置方法图解_无线桥接设置

    随着无线网络的发展,现在越来越多的公司及企业都已经开始布局无线局域网,今天我们主要介绍下适合中小企业的无线路由器桥接或WDS功能.文章以TP-link WR841N无线路由器设置为例,其它路由器参考设 ...

  8. linux中/etc/init.d [转]

    一.关于/etc/init.d 如果你使用过linux系统,那么你一定听说过init.d目录.这个目录到底是干嘛的呢?它归根结底只做了一件事情,但这件事情非同小可,是为整个系统做的,因此它非常重要.i ...

  9. 终于成功仿了一次Kalman滤波器

    终于成功仿了一次Kalman滤波器 首先是测试了从网上down的一段代码 % KALMANF - updates a system state vector estimate based upon a ...

  10. CSS中position:fixed的用法

    我们都知道CSS中定位属性position的值,除了默认的值外,还有absolute,relative和fixed.我平时比较常用absolute和relative,而position:fixed却没 ...