//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. JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-002identity详解

    一.简介 1.You now have three methods for distinguishing references:  Objects are identical if they occ ...

  2. Java:装饰设计模式

    装饰设计模式: 当想要对已有的对象进行功能增强时,可以定义类,将已有对象传入,基于已有的功能, 并提供加强功能,那么自定义的该类就称为装饰类. 装饰类通常通过构造方法接收被装饰的对象,并基于被装饰的对 ...

  3. POJ3267——The Cow Lexicon(动态规划)

    The Cow Lexicon DescriptionFew know that the cows have their own dictionary with W (1 ≤ W ≤ 600) wor ...

  4. POJ3252——Round Number(组合数学)

    Round Numbers DescriptionThe cows, as you know, have no fingers or thumbs and thus are unable to pla ...

  5. left join查询结果大于原始数据

    left join onon后面一定是一个主键或者是一个值为唯一的字段吗  on后面关联的条件如果是1对1的数量就不变,如果是1对多的数量就会增加 追问: 问题就在这,我1对多了 追答: 通常的做法是 ...

  6. 15个必须知道的chrome开发者技巧

    在Web开发者中,Google Chrome是使用最广泛的浏览器.六周一次的发布周期和一套强大的不断扩大开发功能,使其成为了web开发者必备的工具.你可能已经熟悉了它的部分功能,如使用console和 ...

  7. java post 请求

    新公司的分词为post调用方式,以前还没用过post,这次上网查了下,比较简单,但还是写篇博客记录下,代码为网上找的,非原创. package com.chuntent.tool; import ja ...

  8. Android控件系列之CheckBox

    学习目的: 1.掌握在Android中如何建立CheckBox 2.掌握CheckBox的常用属性 3.掌握CheckBox选中状态变换的事件(监听器) CheckBox简介: CheckBox和Bu ...

  9. Weblogic控制器的部署

    WebLogic的安装 一 WebLogic安装 1.  打开WebLogic安装程序:oepe11_wls1031.exe(我们选用的是WebLogic 10.3g).如图1-1所示: 图1-1   ...

  10. RSA (cryptosystem)

    https://en.wikipedia.org/wiki/RSA_(cryptosystem) RSA is one of the first practical实用性的 public-key cr ...